Python Pandas:如何返回groupby的成员

2024-10-01 00:35:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为相对人研究数据帧。但是我找不到兄弟,我找不到办法把他们都写在一个特定的专栏上。下面是一个例子:

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

我很感激任何帮助!你知道吗


Tags: dockernamecommonjohnsmithbrothererickmoon
2条回答
def same_father(me, data):
    hasdad = data.Father == data.at[me, 'Father']
    notme = data.index != me
    isbro = hasdad & notme
    return data.loc[isbro].index.tolist()

df2 = df.set_index('Name')
getbro = lambda x: same_father(x.name, df2)
df2['Brother'] = df2.apply(getbro, axis=1)

我认为这应该行得通

下面是一个您可以尝试的想法,首先创建一个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               []

相关问题 更多 >