[英]Remove groups with more than one unique value in another column
I have an excel like 我有一个像
Name X Y
A 5 9
B 5 10
C 7 9
D 7 9
E 5 10
F 5 8
I want to remove rows that have duplicate values in Y column but different values in X column. 我想删除在Y列中具有重复值但在X列中具有不同值的行。 (In other words if there are more than one values of X for one value of Y, delete all those rows) Result should be: (换句话说,如果一个Y值中有多个X值,则删除所有这些行)结果应为:
Name X Y
B 5 10
E 5 10
F 5 8
Use groupby
with transform
and "nunique"
, and filter out groups with more than 1 unique value in X: 将groupby
与transform
和"nunique"
,并过滤出X中具有1个以上唯一值的组:
df[df.groupby('Y').X.transform('nunique') == 1]
Name X Y
1 B 5 10
4 E 5 10
5 F 5 8
Similar solution, use map
to broadcast the result: 类似的解决方案,使用map
播放结果:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.