简体   繁体   中英

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

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.

(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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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