我已经将数据加载到 Pandas 数据框中。
Example:
Date Price
2012/12/02 141.25
2012/12/05 132.64
2012/12/06 132.11
2012/12/21 141.64
2012/12/25 143.19
2012/12/31 139.66
2013/01/05 145.11
2013/01/06 145.99
2013/01/07 145.97
2013/01/11 145.11
2013/01/12 145.99
2013/01/24 145.97
2013/02/23 145.11
2013/03/24 145.99
2013/03/28 145.97
2013/04/28 145.97
2013/05/24 145.97
2013/06/23 145.11
2013/07/24 145.99
2013/08/28 145.97
2013/09/28 145.97
只有两列,一列是数据,一列是价格。
现在如何对从 2013 年开始到月度和季度 df 的数据进行分组或重新采样?
Monthly:
Date Price
2013/01/01 Monthly total
2013/02/01 Monthly total
2013/03/01 Monthly total
2013/04/01 Monthly total
2013/05/01 Monthly total
2013/06/01 Monthly total
2013/07/01 Monthly total
2013/08/01 Monthly total
2013/09/01 Monthly total
季刊:
Date Price
2013/01/01 Quarterly total
2013/04/01 Quarterly total
2013/07/01 Quarterly total
请注意,月度和季度数据需要从每月的第一天开始,但在原始数据框中缺少每月的第一天数据,每个月的有效每日数据数量可能会有所不同。此外,原始数据框包含 2012 年至 2013 年的数据,我只需要 2013 年初的月度和季度数据。
我尝试过类似的东西
result1 = df.groupby([lambda x: x.year, lambda x: x.month], axis=1).sum()
但不起作用。
谢谢你!
首先将日期列转换为日期时间索引:
df.Date = pd.to_datetime(df.Date)
df.set_index('Date', inplace=True)
然后使用resample
。偏移量别名列表位于熊猫文档。对于月初重新采样,请使用MS
, and QS
对于宿舍:
df.resample('QS').sum()
Out[46]:
Price
Date
2012-10-01 830.49
2013-01-01 1311.21
2013-04-01 437.05
2013-07-01 437.93
df.resample('MS').sum()
Out[47]:
Price
Date
2012-12-01 830.49
2013-01-01 874.14
2013-02-01 145.11
2013-03-01 291.96
2013-04-01 145.97
2013-05-01 145.97
2013-06-01 145.11
2013-07-01 145.99
2013-08-01 145.97
2013-09-01 145.97
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)