Pandas:如何将子分组的DataFrame组合到单个DataFrame中

时间:2014-04-04 18:22:35

标签: python pandas merge

我喜欢根据日期对DataFrame进行分组,然后获取每个组的平均值,然后将它们合并到一个DataFrame中。

 df1= pd.DataFrame({'A' : ['2014-01-01', '2014-01-01', '2014-01-02', '2014-01-03','2014-01-03', '2014-01-04', '2014-01-04', '2014-01-05'],'B' : ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],'C' : np.random.randn(8), 'D' : np.random.randn(8)})
 df1['DT']=pd.to_datetime(df1.A) 
 df1=df1.set_index('DT') # set 'A' as the index

>>> df1
                     A      B         C         D
DT                                               
2014-01-01  2014-01-01    one -0.626296 -0.360708
2014-01-01  2014-01-01    one  0.212051 -1.275909
2014-01-02  2014-01-02    two -0.305094  0.351046
2014-01-03  2014-01-03  three  1.136001  1.029615
2014-01-03  2014-01-03    two -0.801339 -0.084780
2014-01-04  2014-01-04    two  0.683201  1.092694
2014-01-04  2014-01-04    one  0.476437  0.250309
2014-01-05  2014-01-05  three -1.007285  0.420201     

 df2=pd.DataFrame()  # New DataFrame

在df1

中合并2天的数据
 for k in df1.index:
     sub=df1[k+dt.timedelta(days=-1):k].mean()  
     print sub

sub是DataFrame的某种格式,但是如何将它们合并到一个DataFrame df2?

...     print sub
... 
C   -0.207122
D   -0.818309
dtype: float64
C   -0.207122
D   -0.818309
dtype: float64
C   -0.239779
D   -0.428524
dtype: float64
C    0.009856
D    0.431960
dtype: float64
C    0.009856
D    0.431960
dtype: float64
C    0.373575
D    0.571959
dtype: float64
C    0.373575
D    0.571959
dtype: float64
C    0.050784
D    0.587734
dtype: float64

1 个答案:

答案 0 :(得分:1)

如果您想进行上述计算,可以将结果连接到原始帧,如下所示

res = pd.concat([df1[k+dt.timedelta(days=-1):k].mean() for k in df1.index], axis=1)
df1 = pd.concat([df1, res.T.set_index(df1.index)], axis=1)