我有两个数据帧:
df_1 = pd.DataFrame(columns=["pointid","lat","lon"],data=[[1,41.792145,3.046884],[2,41.799834,3.051082],[3,41.813694,3.063463], [4,41.817673,3.067025]])
df_2 = pd.DataFrame(columns=["id","point_from","point_to"],
data=[[1,1,2],[2,1,3],[3,2,3]])
我想将它们合并,以便使用以下列(1行示例)获取新的数据帧df_3
:
id point_from point_to lat_from lon_from lat_to lon_to
1 1 2 41.792145 3.046884 41.799834 3.051082
我该怎么做?
答案 0 :(得分:1)
一种选择是将df_1
与df_2
两次合并在 point_from 和 point_to 列上:
df_1 = df_1.set_index("pointid")
(df_2.merge(df_1.add_suffix("_from"), left_on="point_from", right_index=True)
.merge(df_1.add_suffix("_to"), left_on="point_to", right_index=True))
# id point_from point_to lat_from lon_from lat_to lon_to
#0 1 1 2 41.792145 3.046884 41.799834 3.051082
#1 2 1 3 41.792145 3.046884 41.813694 3.063463
#2 3 2 3 41.799834 3.051082 41.813694 3.063463