Elena Forres
Elena Forres

Reputation: 327

Find rows that have same values in another column - Python

I have a basic Python questions.

I have a pandas dataframe like this:

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. So I would like the output to look something like this:

John Jen 10
Tom Tim 11

Upvotes: 9

Views: 7247

Answers (1)

EdChum
EdChum

Reputation: 393963

IIUC you could do it this way, groupby on 'User_id' and then filter the 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

Upvotes: 11

Related Questions