如果列匹配,则合并数据帧

2024-04-20 05:57:00 发布

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

我需要将df1中的机场名称与df2中的lat和lng匹配

我试过使用join和groupby,但效果不错

df1:

 name     code     lat     lng
 temp0      a1      3.2    121.9
 temp1      a2      4.9    151.6
 temp2      b1      6.7    125.3

df2:

 city       lat      lng     admin  nearest_airport_lat  nearest_airport_lng
 city 1     4.7     132.6    admin1     4.9                  151.6
 city 2     5.1     142.9    admin1     6.7                  125.3
 city 3     2.2     110.4    admin1     3.2                  121.9

预期产出:

  city      lat      lng    admin   nearest_airport_lat nearest_airport_lng   airport
 city 1     4.7     132.6   admin1          4.9                  151.6        temp1

Tags: 名称cityadmindf1df2joinlnglat