删除另一列中具有多个唯一值的组 [英] Remove groups with more than one unique value in another column
本文介绍了删除另一列中具有多个唯一值的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个优秀的人
Name X Y
A 5 9
B 5 10
C 7 9
D 7 9
E 5 10
F 5 8
我想删除在Y列中具有重复值但在X列中具有不同值的行.(换句话说,如果一个Y值中有多个X值,则删除所有这些行)结果应该是:
I want to remove rows that have duplicate values in Y column but different values in X column. (In other words if there are more than one values of X for one value of Y, delete all those rows) Result should be:
Name X Y
B 5 10
E 5 10
F 5 8
推荐答案
使用 groupby
与 transform
和"nunique"
,并在X中过滤出具有1个以上唯一值的组:
Use groupby
with transform
and "nunique"
, and filter out groups with more than 1 unique value in X:
df[df.groupby('Y').X.transform('nunique') == 1]
Name X Y
1 B 5 10
4 E 5 10
5 F 5 8
相似的解决方案,请使用 map
广播结果:
Similar solution, use map
to broadcast the result:
df[df.Y.map(df.groupby('Y').X.nunique()) == 1]
Name X Y
1 B 5 10
4 E 5 10
5 F 5 8
这篇关于删除另一列中具有多个唯一值的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文