简体   繁体   中英

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. (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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM