简体   繁体   English

通过在 Pandas 中计数来分组

[英]Group By Having Count in Pandas

Here is my data:这是我的数据:

{'SystemID': {0: '95EE8B57',
 1: '5F891F03',
 2: '5F891F03',
 3: '5F891F03'},
 'Day': {0: '06/08/2018', 1: '05/08/2018', 2: '04/08/2018',   3: '05/08/2018'},
 'AlarmClass-S': {0: 4, 1: 2, 2: 4, 3: 0},
 'AlarmClass-ELM': {0: 0, 1: 0, 2: 0, 3: 2}}

I would like to perform an aggregation and filtering which in SQL would be formulated as我想执行聚合和过滤,在 SQL 中将被表述为

SELECT SystemID, COUNT(*) as count FROM table GROUP BY SystemID HAVING COUNT(*) > 2

Thus the result shall be因此结果应该是

    {'SystemID': {0: '5F891F03'},
 'count': {0: '3'}}

How to do this in pandas?如何在熊猫中做到这一点?

You can use groupby and count , then filter at the end.您可以使用groupbycount ,然后在最后进行过滤。

(df.groupby('SystemID', as_index=False)['SystemID']
   .agg({'count': 'count'})
   .query('count > 2'))

   SystemID  count
0  5F891F03      3

(df.groupby('SystemID', as_index=False)['SystemID']
   .agg({'count': 'count'})
   .query('count > 2')
   .to_dict())
# {'SystemID': {0: '5F891F03'}, 'count': {0: 3}}

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

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