统计产品在当地的销售数据
目录
1 统计各个年度/季度中,地区、国家、服务分类的销售额和利润数据
import pandas as pd
from pyecharts import options as opts
#中文乱码与坐标轴负号处理
plt.rcParams['font.sans-serif'] = ['Microsoft Yahei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel("非洲通讯产品销售数据.xlsx")
data.head(5)
# 转换为日期格式
data["日期"] = pd.to_datetime(data["日期"])
# 将日期格式设置为索引,按照年月日筛选,此步骤必须有
data1 = data.set_index("日期")
data1
1 统计各个年度/季度中,地区、国家、服务分类的销售额和利润数据
1.1 各年度的销售额和利润
1.1.1 按地区筛选销售额
data_year17 = data1["2017"]
data_year17_diqu = data_year17.groupby(by='地区').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year17_diqu.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year17_diqu.columns=col
data_year17_diqu=data_year17_diqu.reset_index()
data_year17_diqu=data_year17_diqu.rename(columns={'销售额':'2017年销售额','利润':'2017年利润'})
data_year18 = data1["2018"]
data_year18_diqu = data_year18.groupby(by='地区').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year18_diqu.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year18_diqu.columns=col
data_year18_diqu=data_year18_diqu.reset_index()
data_year18_diqu=data_year18_diqu.rename(columns={'销售额':'2018年销售额','利润':'2018年利润'})
data_year19 = data1["2019"]
data_year19_diqu = data_year19.groupby(by='地区').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year19_diqu.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year19_diqu.columns=col
data_year19_diqu=data_year19_diqu.reset_index()
data_year19_diqu=data_year19_diqu.rename(columns={'销售额':'2019年销售额','利润':'2019年利润'})
data_year20 = data1["2020"]
data_year20_diqu = data_year20.groupby(by='地区').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year20_diqu.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year20_diqu.columns=col
data_year20_diqu=data_year20_diqu.reset_index()
data_year20_diqu=data_year20_diqu.rename(columns={'销售额':'2020年销售额','利润':'2020年利润'})
data_diqu = pd.merge(data_year17_diqu,data_year18_diqu,on='地区')
data_diqu = pd.merge(data_diqu,data_year19_diqu,on='地区')
data_diqu = pd.merge(data_diqu,data_year20_diqu,on='地区')
data_diqu
#转置
data_diqu1 = data_diqu.set_index('地区', inplace = False)
data_diqu2 = data_diqu1.T
data_diqu_xiaoshou = data_diqu2.loc[['2017年销售额','2018年销售额','2019年销售额','2020年销售额']]
data_diqu_xiaoshou
from pyecharts.charts import Line
from pyecharts.globals import CurrentConfig, OnlineHostType
CurrentConfig.ONLINE_HOST = OnlineHostType.NOTEBOOK_HOST
attr = list(['2017年销售额','2018年销售额','2019年销售额','2020年销售额'])
v1 = list(data_diqu_xiaoshou['Eastern'])
v2 = list(data_diqu_xiaoshou['Middle'])
v3 = list(data_diqu_xiaoshou['Northern'])
v4 = list(data_diqu_xiaoshou['Southern'])
v5 = list(data_diqu_xiaoshou['Western'])
line = (Line()
.add_xaxis(attr)
.add_yaxis('Eastern', v1)
.add_yaxis('Middle',v2)
.add_yaxis('Northern',v3)
.add_yaxis('Southern',v4)
.add_yaxis('Western',v5)
)
line.render_notebook()
1.1.2 按地区筛选利润
data_diqu_lirun = data_diqu2.loc[['2017年利润','2018年利润','2019年利润','2020年利润']]
from pyecharts.charts import Bar
attr = list(['2017年利润','2018年利润','2019年利润','2020年利润'])
v1 = list(data_diqu_lirun['Eastern'])
v2 = list(data_diqu_lirun['Middle'])
v3 = list(data_diqu_lirun['Northern'])
v4 = list(data_diqu_lirun['Southern'])
v5 = list(data_diqu_lirun['Western'])
Bar = (Bar()
.add_xaxis(attr)
.add_yaxis('Eastern', v1)
.add_yaxis('Middle',v2)
.add_yaxis('Northern',v3)
.add_yaxis('Southern',v4)
.add_yaxis('Western',v5)
)
Bar.render_notebook()
1.1.3 按国家筛选销售额
data_year17_guojia = data_year17.groupby(by='国家').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year17_guojia.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year17_guojia.columns=col
data_year17_guojia=data_year17_guojia.reset_index()
data_year17_guojia=data_year17_guojia.rename(columns={'销售额':'2017年销售额','利润':'2017年利润'})
data_year18_guojia = data_year18.groupby(by='国家').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year18_guojia.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year18_guojia.columns=col
data_year18_guojia=data_year18_guojia.reset_index()
data_year18_guojia=data_year18_guojia.rename(columns={'销售额':'2018年销售额','利润':'2018年利润'})
data_year19_guojia = data_year19.groupby(by='国家').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year19_guojia.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year19_guojia.columns=col
data_year19_guojia=data_year19_guojia.reset_index()
data_year19_guojia=data_year19_guojia.rename(columns={'销售额':'2019年销售额','利润':'2019年利润'})
data_year20_guojia = data_year20.groupby(by='国家').sum()[['销售额','利润']]
#将其转为二维表
col=[]
for i in data_year20_guojia.columns:
i=list(i)
i[1]=str(i[1])
col.append(''.join(i))
data_year20_guojia.columns=col
data_year20_guojia=data_year20_guojia.reset_index()
data_year20_guojia=data_year20_guojia.rename(columns={'销售额':'2020年销售额','利润':'2020年利润'})
展示 2020 年年度销售额前 3 名的国家及其年增长率
data_year20_guojia1 = data_year20_guojia.sort_values(by='2020年销售额',ascending=False)
data_year20_guojia1.head(3)
#合并
data_guojia = pd.merge(data_year17_guojia,data_year18_guojia,on='国家')
data_guojia = pd.merge(data_guojia,data_year19_guojia,on='国家')
data_guojia = pd.merge(data_guojia,data_year20_guojia,on='国家')
from pyecharts.charts import Bar
attr = list(data_guojia['国家'])
v1 = list(data_guojia['2017年销售额'])
v2 = list(data_guojia['2018年销售额'])
v3 = list(data_guojia['2019年销售额'])
v4 = list(data_guojia['2020年销售额'])
Bar = (Bar()
.add_xaxis(attr)
.add_yaxis('2017年销售额', v1,stack="stack0")
.add_yaxis('2018年销售额',v2,stack="stack0")
.add_yaxis('2019年销售额',v3,stack="stack0")
.add_yaxis('2020年销售额',v4,stack="stack0")
.set_global_opts(
#title_opts=opts.TitleOpts(title="国内平台登录次数", subtitle="副标题"), # 标题
toolbox_opts=opts.ToolboxOpts(), # 工具箱
datazoom_opts=opts.DataZoomOpts(range_start=0,range_end=100) # 横轴缩放
)
.set_series_opts(
# 为了不影响标记点,这里把标签关掉
label_opts=opts.LabelOpts(is_show=False))
)
Bar.render_notebook()
1.1.4 按国家筛选利润
from pyecharts.charts import Bar
attr = list(data_guojia['国家'])
v1 = list(data_guojia['2017年利润'])
v2 = list(data_guojia['2018年利润'])
v3 = list(data_guojia['2019年利润'])
v4 = list(data_guojia['2020年利润'])
Bar = (Bar()
.add_xaxis(attr)
.add_yaxis('2017年利润', v1,stack="stack0")
.add_yaxis('2018年利润',v2,stack="stack0")
.add_yaxis('2019年利润',v3,stack="stack0")
.add_yaxis('2020年利润',v4,stack="stack0")
.set_global_opts(
#title_opts=opts.TitleOpts(title="国内平台登录次数", subtitle="副标题"), # 标题
toolbox_opts=opts.ToolboxOpts(), # 工具箱
datazoom_opts=opts.DataZoomOpts(range_start=0,range_end=100) # 横轴缩放
)
.set_series_opts(
# 为了不影响标记点,这里把标签关掉
label_opts=opts.LabelOpts(is_show=False))
)
Bar.render_notebook()
1.1.5 按服务分类筛选销售额
from pyecharts.charts import Bar
attr = list(data_fuwu['服务分类'])
v1 = list(data_fuwu['2017年销售额'])
v2 = list(data_fuwu['2018年销售额'])
v3 = list(data_fuwu['2019年销售额'])
v4 = list(data_fuwu['2020年销售额'])
Bar = (Bar()
.add_xaxis(attr)
.add_yaxis('2017年销售额', v1)
.add_yaxis('2018年销售额',v2)
.add_yaxis('2019年销售额',v3)
.add_yaxis('2020年销售额',v4)
.set_global_opts(
#title_opts=opts.TitleOpts(title="国内平台登录次数", subtitle="副标题"), # 标题
toolbox_opts=opts.ToolboxOpts(), # 工具箱
datazoom_opts=opts.DataZoomOpts(range_start=0,range_end=100) # 横轴缩放
)
.set_series_opts(
# 为了不影响标记点,这里把标签关掉
label_opts=opts.LabelOpts(is_show=False))
)
Bar.render_notebook()
1.1.6 按服务分类筛选利润
1.2 各季度的销售额和利润
1.2.1 按地区筛选销售额和利润
1.2.2 按国家筛选销售额和利润
1.2.3 按服务筛选销售额和利润
from pyecharts import options as opts
from pyecharts.charts import Pie
teacher = ['1季度','2季度','3季度','4季度']
num1 = list(data_jidu1['2017年销售额'][:4])
num2 = list(data_jidu1['2018年销售额'][:4])
num3 = list(data_jidu1['2019年销售额'][:4])
num4 = list(data_jidu1['2020年销售额'][:4])
c = Pie()
# 是否展示成南丁格尔图,通过半径区分数据大小,有'radius'和'area'两种模式。
c.add("2017年销售额",[list(z) for z in zip(teacher,num1)],radius = ["30%","25%"],center=[150,220],rosetype='radius') # radius:扇区圆心角展现数据的百分比,半径展现数据的大小
c.add("2018年销售额",[list(z) for z in zip(teacher,num2)],radius = ["30%","25%"],center=[620,220],rosetype='area') # area:所有扇区圆心角相同,仅通过半径展现数据大小
c.add("2019年销售额",[list(z) for z in zip(teacher,num3)],radius = ["30%","25%"],center=[150,420],rosetype='radius')
c.add("2020年销售额",[list(z) for z in zip(teacher,num4)],radius = ["30%","25%"],center=[620,420],rosetype='area')
# 设置圆环的粗细和大小
c.set_global_opts(title_opts = opts.TitleOpts(title = "图"),legend_opts = opts.LegendOpts(orient = "vertical",pos_top = "5%",pos_left = "2%"))
c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
#c.render()
c.render_notebook()
2 计算各国、各服务分类销售额和利润的同比增长率
2.1 各国销售额同比增长率
#设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)
for name,group in data1.groupby('国家'):
group = group.resample('Y').sum()
group["销售额同比增长率"] = group["销售额"].pct_change(periods=1)
print(name)
print(group)
2.2 各国利润同比增长率
#设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)
for name,group in data1.groupby('国家'):
group = group.resample('Y').sum()
group["利润同比增长率"] = group["利润"].pct_change(periods=1)
print(name)
print(group)
2.3 服务销售额同比增长率
#设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)
for name,group in data1.groupby('服务分类'):
group = group.resample('Y').sum()
group["销售额同比增长率"] = group["销售额"].pct_change(periods=1)
print(name)
print(group)
2.4 服务利润同比增长率
#设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)
for name,group in data1.groupby('服务分类'):
group = group.resample('Y').sum()
group["利润同比增长率"] = group["利润"].pct_change(periods=1)
print(name)
print(group)