pandas :选择groupby.sum()满足条件的行 [英] Pandas: Selecting rows for which groupby.sum() satisfies condition
本文介绍了 pandas :选择groupby.sum()满足条件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在熊猫中,我有一个以下形式的数据框:
In pandas I have a dataframe of the form:
>>> import pandas as pd
>>> df = pd.DataFrame({'ID':[51,51,51,24,24,24,31], 'x':[0,1,0,0,1,1,0]})
>>> df
ID x
51 0
51 1
51 0
24 0
24 1
24 1
31 0
对于每个"ID",多次记录"x"的值,它是0或1.我想从df
中选择那些包含"x"为1的"ID"的行.至少两次.
For every 'ID' the value of 'x' is recorded several times, it is either 0 or 1. I want to select those rows from df
that contain an 'ID' for which 'x' is 1 at least twice.
对于每个"ID",我设法计算"x"为1的次数,
For every 'ID' I manage to count the number of times 'x' is 1, by
>>> df.groupby('ID')['x'].sum()
ID
51 1
24 2
31 0
但是我不知道如何从这里继续.我想要以下输出:
But I don't know how to proceed from here. I would like the following output:
ID x
24 0
24 1
24 1
推荐答案
使用groupby
和filter
df.groupby('ID').filter(lambda s: s.x.sum()>=2)
输出:
ID x
3 24 0
4 24 1
5 24 1
这篇关于 pandas :选择groupby.sum()满足条件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文