2021-08-04——实践项目1(书本案例)

  • Post author:
  • Post category:其他


跟随书本教学将书本案例进行操作

1.概述

电商平台为了促进销售,广告的投入力度巨大,探索以往销售收入与广告费两组数据的关系,然后进行销售收入的预测.

2.分析方法

两组数据存在一定的线性关系、线性回归

3.项目实现

分析销售收入与广告支出

首先将日期数九转化为datetime并显示

销售码洋

日期

2019-01-01  20673.4

2019-01-02  17748.6

2019-01-03  17992.6

2019-01-04  31944.4

2019-01-05  37875.0

…             …

2019-12-27  34826.0

2019-12-28  35001.6

2019-12-29  39068.8

2019-12-30  38963.2

2019-12-31  42659.8

[365 rows x 1 columns]

销售码洋

日期

2019-01   958763.6

2019-02   900500.2

2019-03  1144057.4

2019-04   911718.8

2019-05  1014847.8

2019-06  1515419.0

2019-07  1433418.2

2019-08  1185811.0

2019-09  1138865.0

2019-10  1848853.4

2019-11  2347063.0

2019-12  1560959.6

Process finished with exit code 0

销售数据的分析——折线图

import pandas as pd
import matplotlib.pyplot as plt
df= pd.read_excel('.\data\销售表.xlsx')
df=df[['日期','销售码洋']]
df['日期'] = pd.to_datetime(df['日期'])    #将日期转换为日期格式
df1= df.set_index('日期',drop=True)        #设置日期为索引
#按天统计销售数据
df_d=df1.resample('D').sum().to_period('D')
print(df_d)
#按月统计销售数据
df_m=df1.resample('M').sum().to_period('M')
print(df_m)
df_d.to_excel('result1.xlsx')   #导出结果
df_m.to_excel('result2.xlsx')   #导出结果
#图表字体为黑体,字号为10
plt.rc('font', family='SimHei',size=10)
#绘制子图
fig = plt.figure(figsize=(9,5))
ax=fig.subplots(1,2)   #创建Axes对象
#分别设置图表标题
ax[0].set_title('按天分析销售收入')
ax[1].set_title('按月分析销售收入')
df_d.plot(ax=ax[0],color='r')             #第一个图折线图
df_m.plot(kind='bar',ax=ax[1],color='g')  #第二个图柱形图
#调整图表距上部和底部的空白
plt.subplots_adjust(top=0.95,bottom=0.15)
plt.show()

对销售收入与广告支出进行对比——相关性分析

读取数据——部分数据进行对比

代码:

import pandas as pd
import matplotlib .pyplot as plt
df1=pd.read_excel(r"C:\Users\86157\Desktop\Code\10\data\广告费.xlsx")
df2=pd.read_excel(r"C:\Users\86157\Desktop\Code\10\data\销售表.xlsx")
print(df1.head())
print(df2.head())

投放日期   支出

0 2019-01-01  810

1 2019-01-01  519

2 2019-01-01  396

3 2019-01-01  278

4 2019-01-01  210

日期                 商品名称  成交件数    销售码洋

0 2019-01-01  Python从入门到项目实践(全彩版)    36  3592.8

1 2019-01-01      零基础学Python(全彩版)    28  2234.4

2 2019-01-01         零基础学C语言(全彩版)    20  1396.0

3 2019-01-01        零基础学Java(全彩版)    26  1814.8

4 2019-01-01         SQL即查即用(全彩版)    12   597.6

折线图

import pandas as pd
import matplotlib.pyplot as plt
df1= pd.read_excel('.\data\广告费.xlsx')
df2= pd.read_excel('.\data\销售表.xlsx')
print(df1.head())
print(df2.head())
df1['投放日期'] = pd.to_datetime(df1['投放日期'])
df1= df1.set_index('投放日期',drop=True)
df2=df2[['日期','销售码洋']]
df2['日期'] = pd.to_datetime(df2['日期'])
df2= df2.set_index('日期',drop=True)
# 按月统计金额
df_x=df1.resample('M').sum().to_period('M')
df_y=df2.resample('M').sum().to_period('M')
#x为广告费,y为销售收入
y1=pd.DataFrame(df_x['支出'])
y2=pd.DataFrame(df_y['销售码洋'])
fig = plt.figure()
# 图表字体为黑体,字号为11
plt.rc('font', family='SimHei',size=11)
ax1 = fig.add_subplot(111)                  #添加子图
plt.title('京东电商销售收入与广告费分析折线图')         #图表标题
#图表x轴标题
x=[0,1,2,3,4,5,6,7,8,9,10,11]
plt.xticks(x,['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'])
ax1.plot(x,y1,color='orangered',linewidth=2,linestyle='-',marker='o',mfc='w',label='广告费')
plt.legend(loc='upper left')
ax2 = ax1.twinx()                           #添加一条y轴坐标轴
ax2.plot(x,y2,color='b',linewidth=2,linestyle='-',marker='o',mfc='w',label='销售收入')
plt.subplots_adjust(right=0.85)
plt.legend(loc='upper center')
plt.show()

散点图

书本据:《python数据分析——从入门到实践》

可见书本销售的收入与广告费的支出呈现相关性——线性回归方程



版权声明:本文为weixin_55723202原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。