Python - function similar to VLOOKUP (Excel)

后端 未结 4 1842
鱼传尺愫
鱼传尺愫 2021-02-05 23:00

i am trying to join two data frames but cannot get my head around the possibilities Python has to offer.

First dataframe:

ID MODEL   REQUESTS ORDERS
1  G         


        
4条回答
  •  醉酒成梦
    2021-02-05 23:30

    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
    

提交回复
热议问题