Home > front end >  vlookup in pandas python
vlookup in pandas python

Time:10-26

I have two dataframes I want to check if a column from first dataframe contains values that are in the column of second dataframe, and if it does, create a column and add 1 to the row where it contains a value from first column first df:

A header Another header
First apple
Second orange
third banana
fourth tea

desired output in second df second df:

A header Another header match
First sheet 0
Second chair 0
third apple 1
fourth orange 1

CodePudding user response:

Use Series.isin and convert boolean to 1/0 values by casting:

df2['match'] = df2['Another header'].isin(df1['Another header']).astype(int)

Or by numpy.where:

df2['match'] = np.where(df2['Another header'].isin(df1['Another header']), 1, 0)
  • Related