假設(shè)我有一個這樣的數(shù)據(jù)框:from pandas import DataFrameexample = {'year_month': [201801,201802,201803,201801,201802,201803], 'store_id': [101,101,101,102,102,102], 'tot_employees': [100,200,150,6,7,10], 'hrs_per_employee': [30,35,20,20,18,15] }df = DataFrame(example,columns=["year_month", "store_id", "tot_employees", "hrs_per_employee"])df我想為每個 store_id 使用不同的子圖堆疊子圖:x軸:年月情節(jié)第 1 行:全體員工情節(jié)線 2:每位員工的小時(shí)數(shù)df.plot() 可以嗎?我無法找到正確的 x,y 輸入來獲得我正在尋找的結(jié)果。如果沒有,是否有一個接近的選擇?提前致謝
1 回答

暮色呼如
僅使用
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個贊
import pandas as pd
df = pd.DataFrame(example)
df.year_month = pd.to_datetime(df.year_month, format='%Y%m', exact=True)
df.set_index('year_month', drop=True, inplace=True)
for x in df.store_id.unique(): df[['tot_employees', 'hrs_per_employee']][df.store_id == x].plot(title=f'Store ID: {x}')
僅使用df.groupby
df.groupby('store_id').plot(y=['tot_employees', 'hrs_per_employee'])
添加回答
舉報(bào)
0/150
提交
取消