groupby+堆叠图
计算商品名称和销售数量
# 筛选出四个类别下的商品数据
categories_of_interest = ['Clothing', 'Accessories', 'Footwear', 'Outerwear']
# data['Category']列中的元素是否在categories_of_interest中
filtered_data = data[data['Category'].isin(categories_of_interest)]
filtered_data.groupby('Category')['Item Purchased'].value_counts()

# 商品种类分析
item_counts = filtered_data.groupby('Category')['Item Purchased'].value_counts().unstack(fill_value=0)
item_counts

关于stack()还有unstack()的使用方法和区别请看:一文详解:7个 Pandas stack() 和 unstack() 使用技巧
# 创建堆叠条形图
# kind='bar' 指定了条形图,
# stacked=True 指定了堆叠模式,
# figsize=(16, 12) 设置了图表的大小,
# colormap='viridis' 设置了颜色映射,
# legend=False 禁用了图例。
ax = item_counts.plot(kind='bar', stacked=True, figsize=(16, 12), colormap='viridis', legend=False)
# 设置图表标题和坐标轴标签
ax.set_title('Sales Quantity of Different Items in Each Category', fontsize=16)
ax.set_xlabel('Category', fontsize=12)
ax.set_ylabel('Quantity Sold', fontsize=12)

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



