用年度数据绘制大 pandas 数据框 [英] Plot pandas data frame with year over year data
问题描述
我有一个格式的数据框
value
2000-01-01 1
2000-03-01 2
2000-06-01 15
2000-09-01 3
2000-12-01 7
2001-01-01 1
2001-03-01 3
2001-06-01 8
2001-09-01 5
2001-12-01 3
2002-01-01 1
2002-03-01 1
2002-06-01 8
2002-09-01 5
2002-12-01 19
(索引是日期时间),我需要逐年绘制所有结果以比较每3个月的结果(数据也可以是每月),加上所有年份的平均值.
(index is datetime) and I need to plot all results year over year to compare the results each 3 months (The data can be monthly, too), plus the average of all years.
我可以轻松地分别绘制它们,但是由于有索引,它将根据索引来移动图:
I can easily plot they separately, but because of the index, it will shift the plots according with the index:
fig, axes = plt.subplots()
df['2000'].plot(ax=axes, label='2000')
df['2001'].plot(ax=axes, label='2001')
df['2002'].plot(ax=axes, label='2002')
axes.plot(df["2000":'2002'].groupby(df["2000":'2002'].index.month).mean())
所以这不是期望的结果.我在这里似乎已经找到了一些答案,但是您必须合并,创建多索引并进行绘图.如果其中一个数据帧具有NaN或缺少值,则可能非常麻烦.有熊猫可以做到吗?
So it's not the desired result. I've seem some answers here, but you have to concat, create a multiindex and plot. If one of the data frames has NaNs or missing values, it can be very cumbersome. Is there a pandas way to do it?
推荐答案
这是您想要的吗?您可以在转换后添加均值.
Is this what you want? You can add means after transformation.
df = pd.DataFrame({'value': [1, 2, 15, 3, 7, 1, 3, 8, 5, 3, 1, 1, 8, 5, 19]},
index=pd.DatetimeIndex(['2000-01-01', '2000-03-01', '2000-06-01', '2000-09-01',
'2000-12-01', '2001-01-01', '2001-03-01', '2001-06-01',
'2001-09-01', '2001-12-01', '2002-01-01', '2002-03-01',
'2002-06-01', '2002-09-01', '2002-12-01']))
pv = pd.pivot_table(df, index=df.index.month, columns=df.index.year,
values='value', aggfunc='sum')
pv
# 2000 2001 2002
# 1 1 1 1
# 3 2 3 1
# 6 15 8 8
# 9 3 5 5
# 12 7 3 19
pv.plot()
这篇关于用年度数据绘制大 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!