简体   繁体   English

删除另一列中具有多个唯一值的组

[英]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: groupbytransform"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.

相关问题 如果列在列表中包含超过 x 个值,则删除组 - Remove groups if column contain more than x number of value in a list Pandas Groupby Select 在一列中具有多个唯一值的组 - Pandas Groupby Select Groups that Have More Than One Unique Values in a Column Pandas GroupBy - 仅显示具有多个唯一特征值的组 - Pandas GroupBy - Show only groups with more than one unique feature-value 删除 Pandas 数据框中所有元素的最佳方法是什么,其中一列中的值在另一列中多次存在? - What is the best way to remove all elements in a pandas dataframe where a value in one columns exists more than once in another column? 从 DataFrame 中删除在一列中仅包含一个唯一值的组 - Remove groups from a DataFrame that contain only a single unique value in one column python pandas标志是否每个列中的每个值都有一个以上的唯一行 - python pandas flag if more than one unique row per value in column 如何检查一列中的值是否可以包含多于另一列中的值 - How to check if a value in one column can contain more than value in another column 如何在熊猫数据框中找到与另一列中的多个值相对应的列中具有值的所有行? - How can I find all rows with a value in one column which corresponds to more than one value in another column in a pandas dataframe? 查找列与另一列中多个匹配的位置 - Find where column matches more than one in another column 删除所有观察结果超过N个的组 - Remove all groups with more than N observations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM