简体   繁体   中英

Using Pandas Lambda to compare two text strings from different columns

I have the following dataframe:

df:
name        givenname
 John           John
 Kim            Kimberly

I would like to create a new column with a 1 or 0 depeding on if its a match.

I have tried:

df['match'] = df.name.apply(lambda x: 1 if x == df.givenname else 0)

but I am getting

**ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). ** I feel like this should be something simple but I cannot seem to get it working! Any help would be much appreciated! Thanks

You need to apply the function on the columns name and givenname and in the lambda function x is each row of data, so you access the value of column name and compare it to the value in givenname .

df['match'] = df[['name','givenname']].apply(lambda x: 1 if x.name == x.givenname else 0, axis=1)

use this:-

df['match']=df.apply(lambda x: 1 if x['name'] == x['givenname'] else 0,axis=1)

Now if you print df you will get your desired output:-

    name    givenname   match
0   John    John        1
1   Kim     Kimberly    0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM