Find rows with same values ​​in another column - Python

I have basic Python questions.

I have a pandas dataframe:

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 have the same value for User_id, without returning the names that appear twice. So I would like the result to look something like this:

 John Jen 10 Tom Tim 11 
+6
source share
1 answer

IIUC, you can do it this way, groupby to 'User_id', and then filter out 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 
+8
source

All Articles