使用聚合值的结果创建字段

时间:2018-01-09 13:35:27

标签: python pandas

我有这个df数据帧:

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],
                   'B' : ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],
                   'C' : [1,2,8,2,3,4,8,7],
                   'D' : np.random.randn(8)})


    A   B     C  D
0   foo one   1  1.701138
1   bar one   2  0.771723
2   foo two   8  0.624447
3   bar three 2  1.725457
4   foo two   3  0.742048
5   bar two   4  0.166653
6   foo one   8 -0.605956
7   foo three 7  0.160273

并希望获得此声明的结果:df.groupby('A')['C'].sum()

A
bar     8
foo    27
Name: C, dtype: int64

此声明:df['Sum'] = df.groupby('A')['C'].sum()生成NaN

    A   B     C  D         Sum
0   foo one   1  1.701138  NaN
1   bar one   2  0.771723  NaN
2   foo two   8  0.624447  NaN
3   bar three 2  1.725457  NaN
4   foo two   3  0.742048  NaN
5   bar two   4  0.166653  NaN
6   foo one   8 -0.605956  NaN
7   foo three 7  0.160273  NaN

我怎样才能得到这个

    A   B     C  D         Sum
0   foo one   1  1.701138  27
1   bar one   2  0.771723  8
2   foo two   8  0.624447  27
3   bar three 2  1.725457  8
4   foo two   3  0.742048  27
5   bar two   4  0.166653  8
6   foo one   8 -0.605956  27
7   foo three 7  0.160273  27

0 个答案:

没有答案