我相信需要几个月pivot
的rename
名称而不是数字,对于新的索引值,请使用cumcount
:
d = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May',
6 : 'Jun',7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
g = df.groupby('months').cumcount()
pd.pivot(index=g, columns=df['months'], values=df['value']).rename(columns=d).plot()
详情:
print(pd.pivot(index=g, columns=df['months'], values=df['value']).rename(columns=d))
months Jan Feb Mar Apr
0 50.0 2.0 10.0 5.0
1 80.0 3.0 16.0 20.0
2 120.0 8.0 31.0 40.0
3 140.0 11.0 34.0 50.0
4 NaN 15.0 43.0 75.0
编辑:
对于仅定义几个月的情节使用subset
:
months = ['Mar','Apr']
g = df.groupby('months').cumcount()
pd.pivot(index=g, columns=df['months'], values=df['value']).rename(columns=d)[months].plot()
DataFrame
或通过boolean indexing
and过滤输入中的月份isin
:
df = df[df['months'].isin([3,4])]
g = df.groupby('months').cumcount()
pd.pivot(index=g, columns=df['months'], values=df['value']).rename(columns=d)[months].plot()