Python - a function similar to VLOOKUP (Excel)

I am trying to combine two data frames, but I can not plunge into the possibilities of Python.

The first information frame:

ID MODEL   REQUESTS ORDERS
1  Golf    123      4
2  Passat  34       5
3  Model 3 500      8
4  M3      5        0

Second data frame:

MODEL   TYPE  MAKE
Golf    Sedan Volkswagen
M3      Coupe BMW
Model 3 Sedan Tesla

I want to add another column in the first data frame 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 considered merging, merging, and a map, but all the examples just added the necessary information to the end of the data frame.

+4
source share
3 answers

, insert map Series, df2 ( - MODEL df2 , 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
+3

join VLOOKUP. , MODEL MAKE.

df.join(df1.set_index('MODEL')['MAKE'], on='MODEL')

, VLOOKUP.

0

:

df1.merge(df2[['MODEL', 'MAKE']], how = 'left')

, , - , "MAKE".

0

Source: https://habr.com/ru/post/1666029/


All Articles