Python - 类似于 VLOOKUP (Excel) 的功能 [英] Python - function similar to VLOOKUP (Excel)
本文介绍了Python - 类似于 VLOOKUP (Excel) 的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试连接两个数据框,但无法理解 Python 必须提供的可能性.
i am trying to join two data frames but cannot get my head around the possibilities Python has to offer.
第一个数据帧:
ID MODEL REQUESTS ORDERS
1 Golf 123 4
2 Passat 34 5
3 Model 3 500 8
4 M3 5 0
第二个数据框:
MODEL TYPE MAKE
Golf Sedan Volkswagen
M3 Coupe BMW
Model 3 Sedan Tesla
我想要的是在第一个名为make"的数据框中添加另一列,使其看起来像这样:
What I want is to add another column in the first dataframe called "make" so that it looks like this:
ID MODEL MAKE REQUESTS ORDERS
1 Golf Volkswagen 123 4
2 Passat Volkswagen 34 5
3 Model 3 Tesla 500 8
4 M3 BMW 5 0
我已经看过合并、连接和映射,但所有示例都只是在数据帧的末尾附加了所需的信息.
I already looked at merge, join and map but all examples just appended the required information at the end of the dataframe.
推荐答案
我认为你可以使用 insert
和 map
by Series
使用 df2
创建(如果 MODEL 列中的某个值
缺少 get df2
中的 NaN
):
I think you can use insert
with map
by Series
created with df2
(if some value in column MODEL
in df2
is missing get NaN
):
df1.insert(2, 'MAKE', df1['MODEL'].map(df2.set_index('MODEL')['MAKE']))
print (df1)
ID MODEL MAKE REQUESTS ORDERS
0 1 Golf Volkswagen 123 4
1 2 Passat NaN 34 5
2 3 Model 3 Tesla 500 8
3 4 M3 BMW 5 0
这篇关于Python - 类似于 VLOOKUP (Excel) 的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文