I am wanting to check a pandas dataframe to see if two columns match two unique values. I know have to check one column at a time, but not two at once.
Basically, I want to see if the person's last name is 'Smith' and their first name is either 'John' or 'Tom' all at the same time.
My code:
import pandas as pd
# create dataframe
name = {'last_name': ['smith','smith','jones','parker'], 'first_name': ['john','tom','mary','peter']}
df = pd.DataFrame(name,columns=['last_name', 'first_name'])
# this is what I want to do
# df.loc[df['last_name'] == 'smith' and df['first_name'].isin(['john', 'tom']), 'match'] = 'yes'
# this works by itself
df.loc[df['last_name'] == 'smith', 'match'] = 'yes'
# this works by itself
df.loc[df['first_name'].isin(['john', 'tom']), 'match'] = 'yes'
print(df)
df.loc[(df['last_name'] == 'smith') & (df['first_name'].isin(['john', 'tom'])), 'match'] = 'yes'You need to use the bitwise and operator - i.e.,&.df['match'] = np.where((df['last_name'] == 'smith') & (df['first_name'].isin(['john', 'tom'])), 'yes', 'no')&and enough parentheses is where I went wrong. Thank you!