Volume069
Volume069

Reputation: 63

Compare two pandas df columns with string values

I have two pandas df colums with populated with some names, first df has more name enteries, and second has less, e.g.

id names
1 John Doe
2 Jane Doe
id names
1 John Doe

I need to create a column with True/False in the first df, if names are matching, e.g.

id names match
1 John Doe True
2 Jane Doe False

so far I have tried this two methodes:

df['match'] = (df1.names is df2.names) # always return False
df['match'] = df1.names.isin(df2.names) # compare only first names, eg. John, but doesn't the second

Additionally I have tried to lowercase and remove strings, but still not getting the result

What am I doing wrong?

Upvotes: 1

Views: 1668

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18406

I tested the solution I provided in comment and it works, here is the sample run:

>>> df1.names.isin(df2.names) #Additional call .tolist() isn't even required.
0     True
1    False
Name: names, dtype: bool

If above doesn't work, try this:

>>> df1['names'].apply(lambda x: x in df2.names.tolist())
0     True
1    False
Name: names, dtype: bool

Upvotes: 1

Related Questions