我有一个数据帧,stock_data.head()
:
date open high low close volume Name
0 2013-02-08 15.07 15.12 14.63 14.75 8407500 AAL
1 2013-02-11 14.89 15.01 14.26 14.46 8882000 AAL
2 2013-02-12 14.45 14.51 14.10 14.27 8126000 AAL
3 2013-02-13 14.30 14.94 14.25 14.66 10259500 AAL
4 2013-02-14 14.94 14.96 13.16 13.99 31879900 AAL
下面是一段代码,用于从数据帧生成groupby
命令:
avg_permonth = stock_data.set_index('date').groupby(pd.Grouper(freq='M'))
avg_permonth['volume'].mean()
date
2013-02-28 5.261789e+06
2013-03-31 4.825485e+06
2013-04-30 4.990292e+06
2013-05-31 4.836257e+06
2013-06-30 5.145598e+06
...
2017-10-31 3.903486e+06
2017-11-30 4.133801e+06
2017-12-31 3.919748e+06
2018-01-31 4.486669e+06
2018-02-28 6.249305e+06
Freq: M, Name: volume, Length: 61, dtype: float64
我的问题是,如何将这些结果按年份分割,然后创建date vs volume
的子批次?(我必须先reset_index()
还是可以把index
画成x axis
?)我希望5subplots
从2013-02
到2018-02
我尝试了这个this SO post,但没有给出我想要的结果-它是5个子地块,但每个地块的x axis
都是从2013年到2018年,并且都有相同的地块
我希望第一个子地块上的x axis
是从2013年的第一个日期值到2013年的最后一个日期值,2014年的第二个地块,依此类推
您可以这样做:
相关问题 更多 >
编程相关推荐