Reputation: 879
everyone, I have a data frame such as :
groups name
1 A
1 B
1 C
1 D
2 E
3 F
3 G
4 H
5 I
and from that I would like to only keep in the data frame the values that are alone in a group:
groups name
2 E
4 H
5 I
E,H and I are alone in their respective groups.
I tried:
df[df.groupby(['groups']).count() == 1 ]
But it does not seem to be the solution.
Upvotes: 0
Views: 57
Reputation: 863166
Use GroupBy.transform
for Series with same size like original DataFrame
:
df[df.groupby(['groups'])['name'].transform('size') == 1 ]
Upvotes: 1
Reputation: 402814
Use duplicated
:
df[~df.groups.duplicated(keep=False)]
groups name
4 2 E
7 4 H
8 5 I
Or, drop_duplicates
.
df.drop_duplicates('groups', keep=False)
groups name
4 2 E
7 4 H
8 5 I
Upvotes: 2