我有一個包含三列(Month_Year、SKU_ID、Actual_Demand)的表
我需要創建一個圖來展示我的 10 個 SKU 中每一個的實際需求變化。
資料的結構是這樣的。
| 月_年 | SKU_ID | 實際_需求 |
|---|---|---|
| 2015 年 1 月 | 1 | 56 |
| 2015 年 2 月 | 2 | 70 |
| 2016 年 1 月 | 1 | 23 |
| 2016 年 1 月 | 2 | 56 |
| 2019 年 12 月 | 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 進行預測,當我研究資料時,在我看來,我做的并不聰明。
uj5u.com熱心網友回復:
您可以嘗試使用 Seaborn 并閱讀色調引數。
有關額外資訊,matplotlib 適合繪制 1-2 個變數,而 seaborn 適合繪制 2 個以上的變數。在您的情況下,您有 3 個變數。
import seaborn as sns
#Assume ad is your DataFrame
sns.lineplot(data=ad, x="Month_Year", y="Actual_Demand", hue="SKU_ID")
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384378.html
上一篇:PythonPandas-如何將時間間隔細分為更小的時間間隔?
下一篇:根據另一列中的值創建新的指標列
