用分组值替换列数据

时间:2019-02-18 18:21:37

标签: pandas pandas-groupby

以下是数据:

import pandas as pd
df = pd.DataFrame({
 'A' : ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo','foo','bar','bar','foo','bar'],
 'B' : ['one', 'one', 'two', 'two','two', 'two', 'one', 'two','one','two','one','two','two'],
 '2018-09-28' : np.random.randn(13), 
 '2018-10-06' : np.random.randn(13),
 '2018-10-13' : np.random.randn(13)
            })

df.index = ['se0','se1','se2','se3','se4','se5','se6','se7',"se8",'se9','se10','se11','se12']
df.index.name = 'SE'

为了用“ B”组值总和替换所有值,我尝试这样做:

df_new=df.iloc[:,1:].apply(lambda x : x.groupby('B').sum())

但是它显然失败了。你能帮我吗?

1 个答案:

答案 0 :(得分:2)

您可能要检查transform

df.drop('A',1).groupby('B').transform('sum')
Out[121]: 
      2018-09-28  2018-10-06  2018-10-13
SE                                      
se0     0.766577   -2.250729    1.056958
se1     0.766577   -2.250729    1.056958
se2     2.050151   -1.908386   -1.318890
se3     2.050151   -1.908386   -1.318890
se4     2.050151   -1.908386   -1.318890
se5     2.050151   -1.908386   -1.318890
se6     0.766577   -2.250729    1.056958
se7     2.050151   -1.908386   -1.318890
se8     0.766577   -2.250729    1.056958
se9     2.050151   -1.908386   -1.318890
se10    0.766577   -2.250729    1.056958
se11    2.050151   -1.908386   -1.318890
se12    2.050151   -1.908386   -1.318890