2 回答
TA贡献1789条经验 获得超10个赞
您可以使用concatwithmerge和 left join:
dff = pd.concat([df1, df2, df3])
print (dff)
id city name
0 1010667747 Suzhou Suzhou IFS
1 1010667356 Shenzhen Kingkey 100
2 1010667289 Wuhan Wuhan Center
0 190010 Shenzhen Ping An Finance Centre
1 190012 Guangzhou Guangzhou CTF Finance Centre
2 190015 Beijing China Zun
0 ZY-13 Shanghai Shanghai World Financial Center
1 ZY-15 Hong Kong International Commerce Centre
2 ZY-16 Changsha Changsha IFS Tower T1
df = df.merge(dff,on = ['city', 'name'], how = 'left')
print (df)
df_num city name matched_df_num \
0 1 Shenzhen Kingkey 100 2
1 2 Shenzhen Ping An Finance Centre 2
2 2 Shenzhen Ping An Finance Centre 3
3 2 Guangzhou Guangzhou CTF Finance Centre 3
4 3 Shanghai Shanghai World Financial Center 3
matched_city matched_name similarity_ratio id
0 Shenzhen Ping An Finance Centre 51 1010667356
1 Guangzhou Guangzhou CTF Finance Centre 66 190010
2 Shanghai Shanghai World Financial Center 59 190010
3 Shanghai Shanghai World Financial Center 56 190012
4 Changsha Changsha IFS Tower T1 57 ZY-13
然后再次合并,为避免重复列使用rename:
d = {'city':'matched_city','name':'matched_name', 'id':'matched_id'}
df5 = df.merge(dff.rename(columns=d),on = ['matched_city', 'matched_name'], how = 'left')
print (df5)
df_num city name matched_df_num \
0 1 Shenzhen Kingkey 100 2
1 2 Shenzhen Ping An Finance Centre 2
2 2 Shenzhen Ping An Finance Centre 3
3 2 Guangzhou Guangzhou CTF Finance Centre 3
4 3 Shanghai Shanghai World Financial Center 3
matched_city matched_name similarity_ratio id \
0 Shenzhen Ping An Finance Centre 51 1010667356
1 Guangzhou Guangzhou CTF Finance Centre 66 190010
2 Shanghai Shanghai World Financial Center 59 190010
3 Shanghai Shanghai World Financial Center 56 190012
4 Changsha Changsha IFS Tower T1 57 ZY-13
matched_id
0 190010
1 190012
2 ZY-13
3 ZY-13
4 ZY-16
编辑:您可以DataFrame.assign首先向每个 DataFrame 添加新值,然后也通过此列合并:
dff = pd.concat([df1.assign(df_num=1), df2.assign(df_num=2), df3.assign(df_num=3)])
df = df.merge(dff,on = ['city', 'name','df_num'], how = 'left')
d = {'city':'matched_city','name':'matched_name', 'id':'matched_id','df_num':'matched_df_num'}
df5 = (df.merge(dff.rename(columns=d),
on = ['matched_city', 'matched_name','matched_df_num'],
how = 'left'))
print (df5)
df_num city name matched_df_num \
0 1 Shenzhen Kingkey 100 2
1 2 Shenzhen Ping An Finance Centre 2
2 2 Shenzhen Ping An Finance Centre 3
3 2 Guangzhou Guangzhou CTF Finance Centre 3
4 3 Shanghai Shanghai World Financial Center 3
matched_city matched_name similarity_ratio id \
0 Shenzhen Ping An Finance Centre 51 1010667356
1 Guangzhou Guangzhou CTF Finance Centre 66 190010
2 Shanghai Shanghai World Financial Center 59 190010
3 Shanghai Shanghai World Financial Center 56 190012
4 Changsha Changsha IFS Tower T1 57 ZY-13
matched_id
0 190010
1 190012
2 ZY-13
3 ZY-13
4 ZY-16
TA贡献1859条经验 获得超6个赞
试试这个,它可能会帮助你解决你的问题
df1 = pd.DataFrame(np.array([
[1010667747, 'Suzhou', 'Suzhou IFS'],
[1010667356, 'Shenzhen', 'Kingkey 100'],
[1010667289, 'Wuhan', 'Wuhan Center']]),
columns=['id', 'city', 'name']
)
df2 = pd.DataFrame(np.array([
[190010, 'Shenzhen', 'Ping An Finance Centre'],
[190012, 'Guangzhou', 'Guangzhou CTF Finance Centre'],
[190015, 'Beijing', 'China Zun']]),
columns=['id', 'city', 'name']
)
df3 = pd.DataFrame(np.array([
['ZY-13', 'Shanghai', 'Shanghai World Financial Center'],
['ZY-15', 'Hong Kong', 'International Commerce Centre'],
['ZY-16', 'Changsha', 'Changsha IFS Tower T1']]),
columns=['id', 'city', 'name']
)
df1['df_type'] = 1
df2['df_type'] = 2
df3['df_type'] = 3
df = pd.concat([df1,df2,df3])
df
添加回答
举报