import pandas as pd# Load the provided filesattachment_1 = pdread_excelrCUsers86136DesktopC题1附件1xlsxattachment_2 = pdread_excelrCUsers86136DesktopC题1附件2xlsx# Display the first few rows of each dataset
加载所需文件
attachment_1 = pd.read_excel(r"C:\Users\86136\Desktop\C题(1)\附件1.xlsx") attachment_2 = pd.read_excel(r"C:\Users\86136\Desktop\C题(1)\附件2.xlsx") attachment_3 = pd.read_excel(r"C:\Users\86136\Desktop\C题(1)\附件3.xlsx") attachment_4 = pd.read_excel(r"C:\Users\86136\Desktop\C题(1)\附件4.xlsx")
显示每个数据集的前几行,进行初步检查
attachment_1.head(), attachment_2.head(), attachment_3.head(), attachment_4.head()
合并数据集,根据"单品编码"合并
merged_data = pd.merge(attachment_2, attachment_1, on="单品编码", how="left")
保存合并后的数据集到桌面
merged_data.to_excel(r"C:\Users\86136\Desktop\merged_dataset.xlsx", index=False)
按品类和商品分类销售量
category_sales = merged_data.groupby('分类名称')['销量(千克)'].sum().sort_values(ascending=False) product_sales = merged_data.groupby('单品名称')['销量(千克)'].sum().sort_values(ascending=False)
绘制销售量分布图
fig, ax = plt.subplots(2, 1, figsize=(12, 12))
品类销售量分布图
category_sales.plot(kind='bar', ax=ax[0], color='teal') ax[0].set_title('Total Sales Volume by Category') ax[0].set_ylabel('Sales Volume (kg)') ax[0].set_xlabel('Category Name')
单品销售量分布图(前10个)
product_sales.head(10).plot(kind='bar', ax=ax[1], color='coral') ax[1].set_title('Total Sales Volume by Product (Top 10)') ax[1].set_ylabel('Sales Volume (kg)') ax[1].set_xlabel('Product Name')
plt.tight_layout() plt.show()
根据销售日期和品类分组,计算销售量
category_date_sales = merged_data.groupby(['销售日期', '分类名称'])['销量(千克)'].sum().reset_index()
绘制按时间分类的销售量趋势图
plt.figure(figsize=(16, 8)) for category in category_date_sales['分类名称'].unique(): subset = category_date_sales[category_date_sales['分类名称'] == category] plt.plot(subset['销售日期'], subset['销量(千克)'], label=category)
plt.title('Sales Volume Trend by Category')
原文地址: https://www.cveoy.top/t/topic/i2f0 著作权归作者所有。请勿转载和采集!