Pandas: groupby with condition

谁说我不能喝 提交于 2021-02-07 18:43:27

问题


I have dataframe:

ID,used_at,active_seconds,subdomain,visiting,category
123,2016-02-05 19:39:21,2,yandex.ru,2,Computers
123,2016-02-05 19:43:01,1,mail.yandex.ru,2,Computers
123,2016-02-05 19:43:13,6,mail.yandex.ru,2,Computers
234,2016-02-05 19:46:09,16,avito.ru,2,Automobiles
234,2016-02-05 19:48:36,21,avito.ru,2,Automobiles
345,2016-02-05 19:48:59,58,avito.ru,2,Automobiles
345,2016-02-05 19:51:21,4,avito.ru,2,Automobiles
345,2016-02-05 19:58:55,4,disk.yandex.ru,2,Computers
345,2016-02-05 19:59:21,2,mail.ru,2,Computers
456,2016-02-05 19:59:27,2,mail.ru,2,Computers
456,2016-02-05 20:02:15,18,avito.ru,2,Automobiles
456,2016-02-05 20:04:55,8,avito.ru,2,Automobiles
456,2016-02-05 20:07:21,24,avito.ru,2,Automobiles
567,2016-02-05 20:09:03,58,avito.ru,2,Automobiles
567,2016-02-05 20:10:01,26,avito.ru,2,Automobiles
567,2016-02-05 20:11:51,30,disk.yandex.ru,2,Computers

I need to do

group = df.groupby(['category']).agg({'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec_target'}).reset_index()

but I want to add there condition connected with

df.groupby(['category'])['ID'].count()

and if count for category less than 5, I want to drop this category. I don't know, how can I write this condition there.


回答1:


As EdChum commented, you can use filter:

Also you can simplify aggregation by sum:

df = df.groupby(['category']).filter(lambda x: len(x) >= 5)

group = df.groupby(['category'], as_index=False)['active_seconds']
          .sum()
          .rename(columns={'active_seconds': 'count_sec_target'})
print (group)

      category  count_sec_target
0  Automobiles               233
1    Computers                47

Another solution with reset_index:

df = df.groupby(['category']).filter(lambda x: len(x) >= 5)

group = df.groupby(['category'])['active_seconds'].sum().reset_index(name='count_sec_target')
print (group)
      category  count_sec_target
0  Automobiles               233
1    Computers                47


来源:https://stackoverflow.com/questions/39634175/pandas-groupby-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!