'How can I do an if statement on a pandas dataframe to check multiple columns for specific values?
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
