我正在为亲戚们在数据框上做一些研究。但是当我找到兄弟时,我无法管理,也找不到将它们全部写下来的方法。下面是一个示例:
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
我想要的是:
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
我非常感谢您的帮助!
最佳答案
您可以尝试以下方法,首先创建一个Brother
列,将所有兄弟作为一个列表,包括其自身,然后分别将其删除。该代码可能已经过优化,但是您可以从以下位置开始:
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 []
关于python - Python Pandas:如何返回groupby的成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38161531/