gpt4 book ai didi

python - 根据Python中的多个条件将多个数据帧中的一列合并到另一个数据帧

转载 作者:行者123 更新时间:2023-12-01 08:09:08 25 4
gpt4 key购买 nike

假设我有一个名为 df 的组合数据框,如下所示。每行都有建筑物的信息及其匹配的建筑物的信息。我希望合并 df1、df2 和 df3 中每个建筑物的 id(见下文)。 df_nummatched_df_num 列用于区分建筑物信息来自哪个数据帧,如果它等于 1,则表示它来自 df1, 2 表示来自 df2,3 表示来自 df3

   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
0 Shenzhen Ping An Finance Centre 51
1 Guangzhou Guangzhou CTF Finance Centre 66
2 Shanghai Shanghai World Financial Center 59
3 Shanghai Shanghai World Financial Center 56
4 Changsha Changsha IFS Tower T1 57

我想合并下面 df1df2df3 中的 id 列以获取建筑物名称和匹配的名称:

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']
)

这是我的预期结果:

   df_num       city                             name          id  \
0 1 Shenzhen Kingkey 100 1010667356
1 2 Shenzhen Ping An Finance Centre 190010
2 2 Shenzhen Ping An Finance Centre 190010
3 2 Guangzhou Guangzhou CTF Finance Centre 190012
4 3 Shanghai Shanghai World Financial Center ZY-13

matched_df_num matched_city matched_name \
0 2 Shenzhen Ping An Finance Centre
1 2 Guangzhou Guangzhou CTF Finance Centre
2 3 Shanghai Shanghai World Financial Center
3 3 Shanghai Shanghai World Financial Center
4 3 Changsha Changsha IFS Tower T1

similarity_ratio matched_id
0 51 190010
1 66 190010
2 59 ZY-13
3 56 ZY-13
4 57 ZY-16

如何使用 Pandas 在 df 中插入两个新列 idmatched_id 及其值?感谢您提前提供帮助。

更新:我的解决方案:

df = df.merge(df1, on = ['city', 'name'], how = 'left').merge(df2, on = ['city', 'name'], how = 'left').merge(df3, on = ['city', 'name'], how = 'left')
final_df = df.merge(df1, left_on = ['matched_city', 'matched_name'], right_on = ['city', 'name'], how = 'left').merge(df2, left_on = ['matched_city', 'matched_name'], right_on = ['city', 'name'], how = 'left').merge(df3, left_on = ['matched_city', 'matched_name'], right_on = ['city', 'name'], how = 'left')

df_num city_x name_x 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_x \
0 Shenzhen Ping An Finance Centre 51 1010667356
1 Guangzhou Guangzhou CTF Finance Centre 66 NaN
2 Shanghai Shanghai World Financial Center 59 NaN
3 Shanghai Shanghai World Financial Center 56 NaN
4 Changsha Changsha IFS Tower T1 57 NaN

id_y id_x id_y city_y name_y id_x city_x \
0 NaN NaN NaN NaN NaN 190010 Shenzhen
1 190010 NaN NaN NaN NaN 190012 Guangzhou
2 190010 NaN NaN NaN NaN NaN NaN
3 190012 NaN NaN NaN NaN NaN NaN
4 NaN ZY-13 NaN NaN NaN NaN NaN

name_x id_y city_y \
0 Ping An Finance Centre NaN NaN
1 Guangzhou CTF Finance Centre NaN NaN
2 NaN ZY-13 Shanghai
3 NaN ZY-13 Shanghai
4 NaN ZY-16 Changsha

name_y
0 NaN
1 NaN
2 Shanghai World Financial Center
3 Shanghai World Financial Center
4 Changsha IFS Tower T1

最佳答案

您可以使用concat使用合并和左连接:

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

然后再次合并,为避免重复列,请使用重命名:

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

关于python - 根据Python中的多个条件将多个数据帧中的一列合并到另一个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com