Aman Garg
Aman Garg

Reputation: 33

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

Upvotes: 0

Views: 507

Answers (1)

cs95
cs95

Reputation: 402363

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

Upvotes: 2

Related Questions