I need to iterate over a list and perform a specific operation if the value from the list exists in one of the pandas dataframe column. I tried to do as below, but getting below error
'Error: #The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'
import pandas as pd
people = {
'fname':['Alex','Jane','John'],
'age':[20,15,25],
'sal':[100,200,300]
}
df=pd.DataFrame(people)
check_list=['Alex','John']
for column in check_list:
if (column == df['fname']):
df['new_column']=df['sal']/df['age']
else:
df['new_column']=df['sal']
df
Required output:
fname age sal new_column
Alex 20 100 5 <<-- sal/age
Jane 15 200 200 <<-- sal as it is
John 25 300 12 <<-- sal/age