【发布时间】:2022-01-20 13:24:44
【问题描述】:
我有一个包含三列(Month_Year、SKU_ID、Actual_Demand)的表
我需要创建一个图来展示我的 10 个 SKU 中每个 SKU 的实际需求变化。
数据的结构是这样的。
| Month_Year | SKU_ID | Actual_Demand |
|---|---|---|
| Jan-2015 | 1 | 56 |
| Feb-2015 | 2 | 70 |
| Jan-2016 | 1 | 23 |
| Jan-2016 | 2 | 56 |
| Dec-2019 | 10 | 100 |
到目前为止,我的方法是过滤 10 个 SKU 中的每一个并创建一个单独的图。
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 9 14:31:10 2021
@author: D996FFO
"""
'Importing Relevant Packages'
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
'Load in dataset with historical sales values'
ad = pd.read_excel (r'C:/Users/d996ffo/Model_Data.xlsx', sheet_name= 'Data_For_Python')
#SKU 1
sku_1 = ad.loc[ad['SKU_ID'] == 1]
'Converting the string format month year into a date format'
sku_1['Month_Year'] = pd.to_datetime(sku_1['Month_Year'])
'Set index equal to the date'
sku_1.index = sku_1['Month_Year']
del sku_1['Month_Year']
del sku_1['SKU_ID']
#SKU 2
sku_2 = ad.loc[ad['SKU_ID'] == 2]
'Converting the string format month year into a date format'
sku_2['Month_Year'] = pd.to_datetime(sku_2['Month_Year'])
'Set index equal to the date'
sku_2.index = sku_2['Month_Year']
del sku_2['Month_Year']
del sku_2['SKU_ID']
plt.plot(sku_1, color = 'blue', label = 'SKU 1')
plt.plot(sku_2, color = 'red', label = 'SKU 2')
sns.lineplot(data = sku_2.Actual_Demand)
但一定有比这更好的方法吗?
稍后我想根据每个 SKU 进行预测,当我研究了数据后,在我看来我做的并不聪明。
【问题讨论】:
-
ad = pd.read_excel(...); sns.lineplot(data=ad, x='Month_Year', y='Actual_Demand', hue='SKU_ID') -
这很有帮助,谢谢!
标签: python pandas time-series