如何在pandas中获取多列的count

0aydgbwb  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(150)

我有一个数据集需要计数。

Manager  hire_request   hire_approval   hired   transfer_request   transfer_approval  transfer
 Adam         0               0            0           0                  1              0
 Blake        1               0            0           0                  0              0
 Blake        0               1            0           0                  0              0
 Blake        1               0            0           0                  0              0
 Chris        0               0            0           1                  0              0
 Chris        0               1            0           0                  0              0

我需要经理给我一个计数。我试着做df.groupby(['Manager'])['Manager','hire_request','hire_approval', 'hired', 'transfer_request','transfer_approval', 'transfer'].count()
所需输出:

Manager  hire_request  hire_apporval   hire   transfer_request    transfer_approval  transfer
 Adam         0               0          0            0                   1              0
 Blake        2               1          0            0                   0              0
 Chris        0               1          0            1                   0              0

谢谢你

qojgxg4l

qojgxg4l1#

您可以用途:

cols_to_exclude = ["Manager"] #you can add here more columns if needed

out = df.groupby("Manager")[df.columns.difference(cols_to_exclude, sort=False)].sum()

或者,如果您想聚合所有列,只需用途:

out = df.groupby("Manager").sum()

输出:

print(out)

         hire_request  hire_approval  hired  transfer_request  transfer_approval  transfer
Manager                                                                                   
Adam                0              0      0                 0                  1         0
Blake               2              1      0                 0                  0         0
Chris               0              1      0                 1                  0         0

相关问题