nicmano nicmano - 5 months ago 9
Python Question

Python Pandas: How do I return members of groupby

I´m doing some resarch on a dataframe for people that are relative. But I can´t manage when I find brothers, I can´t find a way to write them down all on a specific column. Here follow an example:

cols = ['Name','Father','Brother']
df = pd.DataFrame({'Brother':'',
'Father':['Erick Moon','Ralph Docker','Erick Moon','Stewart Adborn'],
'Name':['John Smith','Rodolph Ruppert','Mathew Common',"Patrick French"]
},columns=cols)

df
Name Father Brother
0 John Smith Erick Moon
1 Rodolph Ruppert Ralph Docker
2 Mathew Common Erick Moon
3 Patrick French Stewart Adborn


What I want is this:

Name Father Brother
0 John Smith Erick Moon Mathew Common
1 Rodolph Ruppert Ralph Docker
2 Mathew Common Erick Moon John Smith
3 Patrick French Stewart Adborn


I apreciate any help!

Answer

Here is an idea you can try, firstly create a Brother column with all brothers as a list including itself and then remove itself separately. The code could probably be optimized but where you can start from:

import numpy as np
import pandas as pd
df['Brother'] = df.groupby('Father')['Name'].transform(lambda g: [g.values])
def deleteSelf(row):
    row.Brother = np.delete(row.Brother, np.where(row.Brother == row.Name))
    return(row)
df.apply(deleteSelf, axis = 1)

#              Name         Father          Brother
# 0      John Smith     Erick Moon  [Mathew Common]
# 1 Rodolph Ruppert   Ralph Docker               []
# 2   Mathew Common     Erick Moon     [John Smith]
# 3  Patrick French Stewart Adborn               []