用python爬取4332条粽子数据,康康什么粽子最火最贵?
感谢您的阅读,您的关注点赞就是对我最大的支持,谢谢!
官方交流裙:606115027
数据整理
In [1]:
# 导包 import pandas as pd import numpy as np import re
In [2]:
# 导入爬取得到的数据 df = pd.read_csv("/home/kesci/input/dragonfestival2193/粽子.csv", engine='python', encoding='utf-8-sig', header=None) df.columns = ["商品名", "价格", "付款人数", "店铺", "发货地址"] df.head(10)
Out[2]:
商品名 | 价格 | 付款人数 | 店铺 | 发货地址 | |
---|---|---|---|---|---|
0 | 五芳斋粽子礼盒 心悦+18只装咸鸭蛋组合端午节礼品团购嘉兴肉粽子 | 129.0 | 6人付款 | 五芳斋官方旗舰店 | 浙江 嘉兴 |
1 | 北京稻香村端午粽子手工豆沙粽220g*2袋散装豆沙粽香甜软糯豆沙粽 | 44.0 | 8人付款 | 天猫超市 | 上海 |
2 | 五芳斋粽子礼盒装鲜肉咸蛋黄大肉粽嘉兴豆沙甜粽端午团购散装礼品 | 89.9 | 100万+人付款 | 五芳斋官方旗舰店 | 浙江 嘉兴 |
3 | 稻香私房鲜肉粽蛋黄肉粽嘉兴粽子咸鸭蛋礼盒装端午节送礼特产团购 | 138.0 | 1936人付款 | 稻香村食品旗舰店 | 北京 |
4 | 嘉兴粽子 蛋黄鲜肉粽新鲜大肉粽早餐散装团购浙江特产蜜枣多口味 | 3.8 | 9500+人付款 | 城城喂食猫 | 浙江 嘉兴 |
5 | 嘉兴特产粽子礼盒装甜咸粽8粽4味真空手工农家粽端午节团购 | 58.8 | 17人付款 | chenyan30151467 | 浙江 嘉兴 |
6 | 五芳斋华礼竹篮礼盒1360g蛋粽组合端午礼品嘉兴粽子礼盒 | 159.0 | 1028人付款 | 天猫超市 | 上海 |
7 | 五芳斋 140g*8只大粽子 福韵端午豆沙蜜枣蛋黄粽新包装送礼礼盒 | 79.9 | 9000+人付款 | 天猫超市 | 上海 |
8 | 真真老老嘉情礼盒10粽6蛋1.52kg/盒嘉兴粽子端午节粽子礼盒装 | 109.0 | 2117人付款 | 天猫超市 | 上海 |
9 | 五芳斋嘉兴粽子新鲜量贩蛋黄肉粽豆沙粽悦喜散装端午特产600g*2袋 | 59.9 | 1349人付款 | 天猫超市 | 上海 |
In [3]:
# 去除重复值 df.drop_duplicates(inplace=True)
In [4]:
# 处理购买人数为空的记录 df['付款人数']=df['付款人数'].replace(np.nan,'0人付款')
In [5]:
# 提取数值 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']
In [6]:
# 删除无发货地址的商品,并提取省份 df = df[df['发货地址'].notna()] df['省份'] = df['发货地址'].str.split(' ').apply(lambda x:x[0])
In [7]:
# 删除多余的列 df.drop(['付款人数', '发货地址', 'num', 'unit'], axis=1, inplace=True) # 重置索引 df = df.reset_index(drop=True) df.head(10) #df.to_csv('清洗完成数据.csv')
Out[7]:
商品名 | 价格 | 店铺 | 销量 | 省份 | |
---|---|---|---|---|---|
0 | 五芳斋粽子礼盒 心悦+18只装咸鸭蛋组合端午节礼品团购嘉兴肉粽子 | 129.0 | 五芳斋官方旗舰店 | 6.0 | 浙江 |
1 | 北京稻香村端午粽子手工豆沙粽220g*2袋散装豆沙粽香甜软糯豆沙粽 | 44.0 | 天猫超市 | 8.0 | 上海 |
2 | 五芳斋粽子礼盒装鲜肉咸蛋黄大肉粽嘉兴豆沙甜粽端午团购散装礼品 | 89.9 | 五芳斋官方旗舰店 | 1000000.0 | 浙江 |
3 | 稻香私房鲜肉粽蛋黄肉粽嘉兴粽子咸鸭蛋礼盒装端午节送礼特产团购 | 138.0 | 稻香村食品旗舰店 | 1936.0 | 北京 |
4 | 嘉兴粽子 蛋黄鲜肉粽新鲜大肉粽早餐散装团购浙江特产蜜枣多口味 | 3.8 | 城城喂食猫 | 9500.0 | 浙江 |
5 | 嘉兴特产粽子礼盒装甜咸粽8粽4味真空手工农家粽端午节团购 | 58.8 | chenyan30151467 | 17.0 | 浙江 |
6 | 五芳斋华礼竹篮礼盒1360g蛋粽组合端午礼品嘉兴粽子礼盒 | 159.0 | 天猫超市 | 1028.0 | 上海 |
7 | 五芳斋 140g*8只大粽子 福韵端午豆沙蜜枣蛋黄粽新包装送礼礼盒 | 79.9 | 天猫超市 | 9000.0 | 上海 |
8 | 真真老老嘉情礼盒10粽6蛋1.52kg/盒嘉兴粽子端午节粽子礼盒装 | 109.0 | 天猫超市 | 2117.0 | 上海 |
9 | 五芳斋嘉兴粽子新鲜量贩蛋黄肉粽豆沙粽悦喜散装端午特产600g*2袋 | 59.9 | 天猫超市 | 1349.0 | 上海 |
In [8]:
df1 = df.sort_values(by="价格", axis=0, ascending=False) df1.iloc[:5,:]
Out[8]:
商品名 | 价格 | 店铺 | 销量 | 省份 | |
---|---|---|---|---|---|
2779 | 御茶膳房端午节礼品黄酒有机粽子咸鸭蛋手提竹篮礼盒装高端大礼包 | 1780.0 | 御茶膳房旗舰店 | 12.0 | 北京 |
2491 | 御茶膳房端午节礼品有机粽子咸鸭蛋手提竹篮礼盒装高端大礼包 | 1088.0 | 御茶膳房旗舰店 | 9.0 | 北京 |
152 | 端午节礼品礼盒装组合高档有机粽子咸鸭蛋送领导高端送礼大礼包 | 858.0 | 御茶膳房旗舰店 | 45.0 | 北京 |
784 | 嘉兴粽子新鲜大肉粽蛋黄肉粽甜粽竹篮粽子礼盒装组合端午送礼团购 | 857.9 | 千味食代i | 2491.0 | 浙江 |
2710 | 米旗端午节粽子礼盒壹品私房多口味肉粽素棕海鲜粽高档礼盒送礼 | 678.0 | 壹品优店 | 5.0 | 北京 |
数据可视化
分词
In [9]:
import jieba import jieba.analyse txt = df['商品名'].str.cat(sep='。') # 添加关键词 jieba.add_word('粽子', 999, '五芳斋') # 读入停用词表 stop_words = [] with open('/home/kesci/input/stop6931/哈工大停用词表.txt', 'r', encoding='gb18030') as f: lines = f.readlines() for line in lines: stop_words.append(line.strip()) # 添加停用词 stop_words.extend(['logo', '10', '100', '200g', '100g', '140g', '130g']) # 评论字段分词处理 word_num = jieba.analyse.extract_tags(txt, topK=100, withWeight=True, allowPOS=()) # 去停用词 word_num_selected = [] for i in word_num: if i[0] not in stop_words: word_num_selected.append(i) key_words = pd.DataFrame(word_num_selected, columns=['words','num'])
Building prefix dict from the default dictionary ... Dumping model to file cache /tmp/jieba.cache Loading model cost 0.789 seconds. Prefix dict has been built succesfully.
查询你的pyecharts版本
In [10]:
import pyecharts print(pyecharts.__version__)
1.1.0
商品销量排名top10 - 柱形图
In [11]:
# 导入包 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='1350px', height='750px')) bar0.add_xaxis(shop_top10.index.tolist()) bar0.add_yaxis('sales_num', shop_top10.values.tolist()) bar0.set_global_opts(title_opts=opts.TitleOpts(title='粽子商品销量Top10'), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), visualmap_opts=opts.VisualMapOpts(max_=shop_top10.values.max())) bar0.render_notebook()
Out[11]:
# 导入包 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='1350px', height='750px')) bar1.add_xaxis(shop_top10.index.tolist()) bar1.add_yaxis('sales_num', shop_top10.values.tolist()) bar1.set_global_opts(title_opts=opts.TitleOpts(title='粽子店铺销量Top10'), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), visualmap_opts=opts.VisualMapOpts(max_=shop_top10.values.max())) bar1.render_notebook()
Out[12]:
不同价格区间的销量占比
from pyecharts.charts import Pie def price_range(x): #按照淘宝推荐划分价格区间 if x <= 22: return '22元以下' elif x <= 115: return '22-115元' elif x <= 633: return '115-633元' else: return '633元以上' 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='1350px', height='750px')) # 内置富文本 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"), toolbox_opts=opts.ToolboxOpts(), title_opts=opts.TitleOpts(title='不同价格区间的粽子销量占比')) pie1.render_notebook()
[['115-633元', 150371.0], ['22-115元', 4297601.0], ['22元以下', 1080993.0], ['633元以上', 2562.0]]
Out[14]:
商品标题文本分析 - 词云图
from pyecharts.charts import WordCloud from pyecharts.globals import SymbolType # 词云图 word1 = WordCloud(init_opts=opts.InitOpts(width='1350px', height='750px')) word1.add("", [*zip(key_words.words, key_words.num)], word_size_range=[20, 200], shape=SymbolType.DIAMOND) word1.set_global_opts(title_opts=opts.TitleOpts('粽子商品名称词云图'), toolbox_opts=opts.ToolboxOpts()) word1.render_notebook()