python对月饼数据可视化
魔王不会哭 人气:0前言
中秋节,又称拜月节、月光诞、月夕等,节期在每年的农历八月十五日(九月十)。
中秋节自古以来就有祭月、赏月、吃月饼、玩花灯、赏桂花、饮桂花酒等民俗,流传经久不息。
马上有临近中秋,这不得好好准备~于是准备对月饼数据进行可视乎
数据
代码
# 导包 import pandas as pd import numpy as np import re
# author:Dragon少年 # 导入爬取得到的数据 df = pd.read_csv("月饼.csv", encoding='utf-8-sig', header=None) df.columns = ["商品名", "价格", "购买人数", "店铺", "地址"] # 去除重复的数据 df.drop_duplicates(inplace=True) print(df.shape) # 删除购买人数0的记录 df['购买人数'] = df['购买人数'].replace(np.nan,'0人付款') df['num'] = [re.findall(r'(\d+\.{0,1}\d*)', i)[0] for i in df['购买人数']] # 提取数值 df['num'] = df['num'].astype('float') # 转化数值型 # 提取单位(万) df['unit'] = [''.join(re.findall(r'(万)', i)) for i in df['购买人数']] # 提取单位(万) df['unit'] = df['unit'].apply(lambda x:10000 if x=='万' else 1) # 计算销量 df['销量'] = df['num'] * df['unit'] # 删除没有发货地址的店铺数据 获取省份 df = df[df['地址'].notna()] df['省份'] = df['地址'].str.split(' ').apply(lambda x:x[0]) # 删除多余的列 df.drop(['购买人数', '地址', 'num', 'unit'], axis=1, inplace=True) # 重置索引 df = df.reset_index(drop=True) df.to_csv('月饼清洗数据.csv')
# 导入包 from pyecharts.charts import Bar from pyecharts import options as opts # 计算月饼总销量Top10的店铺 shop_top10 = df.groupby('店铺')['销量'].sum().sort_values(ascending=False).head(10) # 绘制柱形图 bar1 = Bar(init_opts=opts.InitOpts(width='600px', height='450px')) bar1.add_xaxis(shop_top10.index.tolist()) bar1.add_yaxis('销量', shop_top10.values.tolist()) bar1.set_global_opts(title_opts=opts.TitleOpts(title='销量Top10店铺-Dragon少年'), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30))) bar1.render("销量Top10店铺-Dragon少年.html") bar1.render_notebook()
# 导入包 from pyecharts.charts import Bar from pyecharts import options as opts # 计算销量top10月饼 shop_top10 = df.groupby('商品名')['销量'].sum().sort_values(ascending=False).head(10) # 绘制柱形图 bar0 = Bar(init_opts=opts.InitOpts(width='750px', height='450px')) bar0.add_xaxis(shop_top10.index.tolist()) bar0.add_yaxis('销量', shop_top10.values.tolist()) bar0.set_global_opts(title_opts=opts.TitleOpts(title='销量Top10月饼-Dragon少年'), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30))) bar0.render("销量Top10月饼-Dragon少年.html") bar0.render_notebook()
from pyecharts.charts import Pie def price_range(x): #按照淘宝推荐划分价格区间 if x <= 50: return '50元以下' elif x <= 150: return '50-150元' elif x <= 500: return '150-500元' else: return '500元以上' df['price_range'] = df['价格'].apply(lambda x: price_range(x)) price_cut_num = df.groupby('price_range')['销量'].sum() data_pair = [list(z) for z in zip(price_cut_num.index, price_cut_num.values)] print(data_pair) # 饼图 pie1 = Pie(init_opts=opts.InitOpts(width='750px', height='350px')) # 内置富文本 pie1.add( series_name="销量", radius=["35%", "55%"], data_pair=data_pair, label_opts=opts.LabelOpts(formatter='{b}—占比{d}%'), ) pie1.set_global_opts(legend_opts=opts.LegendOpts(pos_left="left", pos_top='30%', orient="vertical"), title_opts=opts.TitleOpts(title='不同价格月饼销量占比-Dragon少年')) pie1.render("不同价格月饼销量占比-Dragon少年.html") pie1.render_notebook()
from pyecharts.charts import Map # 计算销量 province_num = df.groupby('省份')['销量'].sum().sort_values(ascending=False) # 绘制地图 map1 = Map(init_opts=opts.InitOpts(width='950px', height='600px')) map1.add("", [list(z) for z in zip(province_num.index.tolist(), province_num.values.tolist())], maptype='china' ) map1.set_global_opts(title_opts=opts.TitleOpts(title='各省月饼销量分布-Dragon少年'), visualmap_opts=opts.VisualMapOpts(max_=1500000) ) map1.render("各省月饼销量分布-Dragon少年.html") map1.render_notebook()
效果
尾语
加载全部内容