[英]Find rows that have same values in another column - Python
I have a basic Python questions. 我有一个基本的Python问题。
I have a pandas dataframe like this: 我有一个像这样的pandas数据帧:
ID | Name | User_id
---+------+--------
1 John 10
2 Tom 11
3 Sam 12
4 Ben 13
5 Jen 10
6 Tim 11
7 Sean 14
8 Ana 15
9 Sam 12
10 Ben 13
I want to get the names and user ids that share the same value for User_id, without returning names that appear twice. 我想获取与User_id共享相同值的名称和用户ID,而不返回出现两次的名称。 So I would like the output to look something like this:
所以我希望输出看起来像这样:
John Jen 10
Tom Tim 11
IIUC you could do it this way, groupby
on 'User_id' and then filter the groupby: IIUC你可以这样来做,
groupby
上“USER_ID”,然后过滤GROUPBY:
In [54]:
group = df.groupby('User_id')['Name'].unique()
In [55]:
group[group.apply(lambda x: len(x)>1)]
Out[55]:
User_id
10 [John, Jen]
11 [Tom, Tim]
Name: Name, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.