2016-11-11 131 views
2

我已經將我的數據加載到Pandas數據框中。Python,將數據框中的每日數據彙總爲每月和每季度

實施例:

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?

每月:

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() 

,但不起作用。

謝謝!

回答

6

首先轉換您的日期列到日期時間指數:

df.Date = pd.to_datetime(df.Date) 
df.set_index('Date', inplace=True) 

然後使用resample。偏移量別名列表位於pandas documentation中。對於月份重新採樣的開始,請使用MSQS作爲季度:

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 
+0

太棒了!我在groupby函數,lambda表達式上掙扎了2天......非常感謝! – Windtalker

+0

因此,如果我有重複的日期,那麼df.set_index仍然有效?或者我需要先處理重複的數據數據? – Windtalker

+0

無關緊要,試試看,改變樣品中的日期以獲得一個愚蠢,你會看到一切都會按預期工作。 – Boud

相關問題