简体   繁体   English

在 Pandas 中计算和广播计数(使用 groupby 变换)

[英]Compute and broadcast a count in pandas (with groupby transform)

How can I compute and broadcast a count in pandas?如何在 Pandas 中计算和广播计数?

To compute a count:要计算计数:

df.groupby('field').size()

To broadcast an aggregation to the original dataframe:要将聚合广播到原始数据帧:

df.groupby('field')['field_to_aggregate'].transform(aggregation)

The latter works if I specify the field to aggregate onto and aggregations like sum , mean , etc. But I am not finding a way to make it work when I want a simple count of the grouped-by field.如果我指定要聚合的字段和summean等聚合,后者会起作用。但是当我想要对分组字段进行简单计数时,我没有找到使其工作的方法。

(Note: I could just use the first and re-join on the original table against the grouped-by table, but I want to avoid joins and I'm looking for an efficient solution that uses pandas' transform ) (注意:我可以只使用第一个并重新加入原始表上的分组表,但我想避免加入,我正在寻找使用熊猫transform的有效解决方案)

You could try:你可以试试:

result = df.groupby('field')['field_to_aggregate'].transform('size')

Note that 'field_to_aggregate' can be the same as 'field' .请注意, 'field_to_aggregate'可以与'field'相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM