2016-04-02 71 views
1

我想利用numpy的從下方陣列計算每個季度的一組值的平均值:如何按季度分組並使用numpy從數組中計算平均值?

Data = [{'date':'2015-01-01',value:5},{'date':'2015-02-01',value:6},{'date':'2015-03-01',value:7},{'date':'2015-04-01',value:8},{'date':'2015-05-01',value:9},{'date':'2015-06-01',value:10},{'date':'2015-07-01',value:11},{'date':'2015-08-01',value:12}] 

我想結果告訴我下面的:

  • 對於Q1-15,平均爲6
  • 對於Q2-15,平均爲9
  • 對於Q3-15,平均爲11.5

基於this stackoverflow question,我已經試過以下:

np = Data #I'm not sure how to read in data into numpy from an array in my structure 
np.resample('Q',how='mean') #I'm not sure if using 'Q' would group this by quarter 
+0

檢查大熊貓。你鏈接到的問題使用熊貓。 – dbliss

回答

1

我認爲大熊貓能在這種情況下更好。我只會用你的簡單例子來說明。

import pandas as pd # use recent version which has dt.quarter attr for time 
import json 
value = 'value' # to be able to read your Data string as json 
Data1 = json.dumps(Data) # need it to use read_json() method. 
a = pd.read_json(Data1) 
a[a['date'].dt.quarter == 1].mean() # 1st quarter 
a[a['date'].dt.quarter == 2].mean() # 2nd quarter 
a[a['date'].dt.quarter == 3].mean() # 3rd quarter