Python 如何识别唯一ID';只有1个真实条件的s?
简单问题:如何识别只有1个真实条件的唯一IDPython 如何识别唯一ID';只有1个真实条件的s?,python,pandas,numpy,Python,Pandas,Numpy,简单问题:如何识别只有1个真实条件的唯一ID Index ID value condition 0 1 1 False 1 1 3 True 2 1 2 False 3 1 1 False 4 2 3 True 5 2 4 True 6 2 5 True 在上述情况下,ID 1(1真)将仅被识别,而ID 2(3真)不会被识别 我将如何编辑下面的代码?我需要
Index ID value condition
0 1 1 False
1 1 3 True
2 1 2 False
3 1 1 False
4 2 3 True
5 2 4 True
6 2 5 True
在上述情况下,ID 1(1真)将仅被识别,而ID 2(3真)不会被识别
我将如何编辑下面的代码?我需要将原始索引和ID保存在分段数据框中
df[df['condition']==True]['ID'].unique()
预期产出:
Index ID value condition
1 1 3 True
祝你一切顺利
感谢您抽出时间。使用
过滤器
df.groupby('ID').filter(lambda x : sum(x['condition'])==1)
Out[685]:
Index ID value condition
0 0 1 1 False
1 1 1 3 True
2 2 1 2 False
3 3 1 1 False
使用
过滤器
df.groupby('ID').filter(lambda x : sum(x['condition'])==1)
Out[685]:
Index ID value condition
0 0 1 1 False
1 1 1 3 True
2 2 1 2 False
3 3 1 1 False