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.
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
将GroupBy.transform
用于具有与原始DataFrame
相同大小的DataFrame
:
df[df.groupby(['groups'])['name'].transform('size') == 1 ]
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.