Pandas Groupby在两列上具有多个过滤器

时间:2019-04-10 05:42:02

标签: pandas-groupby

df = pd.DataFrame({'cost_center':['A', 'A', 'A', 'B', 'B','B','C', 'C', 'C', 'D', 'D','D'],
               'cost_type': ['payroll','depreciation', 'software','payroll','depreciation', 'software','payroll','depreciation', 'software','payroll','depreciation', 'software'],
               'the_amount': [0,11,11,11,11,11,11,11,11,0,11,11,]})

output
cost_center   cost_type      the_amount

A              payroll          0
A              depreciation    11
A              software        11
B              payroll         11
B              depreciation    11
B              software        11
C              payroll         11
C              depreciation    11
C              software        11
D              payroll         0
D              depreciation    11
D              software        11

我只希望将工资中心的cost_type的the_amount = 0的cost_centers的the_amount求和

我尝试过类似的事情

df_cost_centers_without_payroll=df.groupby('Cost Center').filter(lambda x: x['cost_type'] == 'payroll'  & x['the_amount'] == 0 )

我希望看到类似下面的内容。由于只有cost_centers的“ A”和“ D”的工资总额为0,因此只会显示A和D。

cost_center         amount
A                      22
D                      22

0 个答案:

没有答案