Pandas使用Groupby和Sum创建列,并附加条件

时间:2016-10-19 10:55:20

标签: python pandas dataframe

我正在尝试在分组后添加pandas DataFrame中的新列以及其他条件

df = pd.DataFrame({ 
    'A'  :[4,5,7,8,2,3,5,2,1,1,4,4,2,4,5,1,3,9,7,9], 
    'B'  :[9,5,7,8,3,3,5,2,1,1,4,4,2,4,5,1,3,5,7,9], 
    'C' :[9,5,7,8,3,3,5,2,1,1,4,4,2,4,5,1,3,5,7,9], 
    'D' :[1,0,1,0,1,1,0,0,1,1,0,0,0,1,1,1,0,0,1,0] 
})
df1 = df.groupby(['A', 'B'], as_index=False).transform('sum')
df1 = df.join(df.groupby(['A'])['C'].sum(), on='A', rsuffix='_inward')

df1

在上面的查询中,它能够求和并给出输出,但如何为df['D'] == 1添加条件

预期输出

    A  B  C  D  C_inward
0   4  9  9  1        13
2   7  7  7  1        14
4   2  3  3  1         3
5   3  3  3  1         3
8   1  1  1  1         3
9   1  1  1  1         3
13  4  4  4  1        13
14  5  5  5  1        5
15  1  1  1  1         3
18  7  7  7  1        14

1 个答案:

答案 0 :(得分:0)

您可以添加boolean indexing

mask = df['D'] == 1
df1 = df[mask].join(df[mask].groupby(['A'])['C'].sum(), on='A', rsuffix='_inward')

print (df1)
    A  B  C  D  C_inward
0   4  9  9  1        13
2   7  7  7  1        14
4   2  3  3  1         3
5   3  3  3  1         3
8   1  1  1  1         3
9   1  1  1  1         3
13  4  4  4  1        13
14  5  5  5  1         5
15  1  1  1  1         3
18  7  7  7  1        14