[英]Get appropriate data for each row in pandas dataframe from another dataframe
Lets say I have two dataframes in pandas. 可以说我在熊猫中有两个数据框。 One
df1
contains airports with other informations and I have another dataframe df2
which contains the location of these airports (latitutde,longitude). 一个
df1
包含具有其他信息的机场,而我还有另一个数据df2
,其中包含这些机场的位置(纬度,经度)。 I would like to assign to each row in df1 the appropriate data from df2
, how can I do that more elegantly and faster than looping over all the possibilities? 我想将
df2
的适当数据分配给df1中的每一行,与遍历所有可能性相比,我该如何更优雅,更快速地做到这一点? Two example image is shown below: 下面是两个示例图像:
ORIGIN DEST DIVERTED
0 ANC SEA 0
1 LAX PBI 0
2 SFO CLT 1
IATA LATITUDE LONGITUDE
17 ANC 61.17432 -149.99619
277 SEA 47.44898 -122.30931
176 LAX 33.94254 -118.40807
235 PBI 26.68316 -80.09559
278 SFO 37.619 -122.37484
66 CLT 35.21401 -80.94313
Lets say you have the following two dataframes: 假设您有以下两个数据框:
df1: DF1:
IATA info1 info2 info3
0 ABI bla bla bla
1 ABE bla bla bla
df2: DF2:
IATA lat long
0 ABE 40 90
1 ABI 20 100
You can use the following code to merge these two dataframes: 您可以使用以下代码合并这两个数据框:
dfNew = pd.merge(df1, df2, on='IATA', how='left')
Output: 输出:
IATA info1 info2 info3 lat long
0 ABI bla bla bla 20 100
1 ABE bla bla bla 40 90
In your case the following will do: 在您的情况下,将执行以下操作:
dfNew = pd.merge(df1, df2, left_on='ORIGIN', right_on='IATA', how='left').drop('IATA', axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.