Equalizer Excel VLOOKUP in pandas

I have the following data frame:

ABC Index 2001-06-30 100 2001-08-31 (=value of A at date B) 2001-07-31 200 2001-09-30 ... 2001-08-31 300 2001-10-31 ... 2001-09-30 400 2001-11-30 ... 

Column B consists of dates from column A , pushed forward by some. I would like to create a column C that consists of the values ​​from column A to date B (It is desirable in logic that this makes the excel VLOOKUP formula. I am not looking here just for a shift (-2), because in reality the shift between B and Index not always equal).

I tried df.loc['B', 'A'] , but this most likely simplified and produced an error.

+6
source share
1 answer

I think you need map on column A :

 df['C'] = df.B.map(df.A) print (df) ABC Index 2001-06-30 100 2001-08-31 300.0 2001-07-31 200 2001-09-30 400.0 2001-08-31 300 2001-10-31 NaN 2001-09-30 400 2001-11-30 NaN 

This is the same as:

 df['C'] = df.B.map(df.A.to_dict()) print (df) ABC Index 2001-06-30 100 2001-08-31 300.0 2001-07-31 200 2001-09-30 400.0 2001-08-31 300 2001-10-31 NaN 2001-09-30 400 2001-11-30 NaN 
+12
source

All Articles