1 回答
TA贡献1893条经验 获得超10个赞
过滤和合并就可以了。
df = pd.DataFrame({'Household Number':[str(i) for i in range(10)],
'Relationship Number':[str(i) for i in range(5)]*2,
'RG Officer Number':np.random.randint(1,100,10),
'RG Officer Name':['name'+str(i) for i in np.random.randint(1,100,10)]})
df
# Household Number Relationship Number RG Officer Number RG Officer Name
#0 0 0 28 name87
#1 1 1 18 name71
#2 2 2 69 name8
#3 3 3 83 name64
#4 4 4 88 name36
#5 5 0 25 name89
#6 6 1 51 name76
#7 7 2 29 name80
#8 8 3 61 name27
#9 9 4 2 name95
df_filtered = df.loc[df['Household Number'] == df['Relationship Number']]
df_filtered
# Household Number Relationship Number RG Officer Number RG Officer Name
#0 0 0 28 name87
#1 1 1 18 name71
#2 2 2 69 name8
#3 3 3 83 name64
#4 4 4 88 name36
df_merged = pd.merge(left=df,right=df_filtered[['Relationship Number','RG Officer Number','RG Officer Name']],
how='left',
on='Relationship Number',suffixes=('_old','_new'))
这是合并的数据。
添加回答
举报