【发布时间】:2020-09-07 00:41:53
【问题描述】:
如果列表中的值存在于 pandas 数据框列之一中,我需要遍历列表并执行特定操作。我尝试如下做,但得到以下错误
'Error: #Series的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 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
所需输出:
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
【问题讨论】:
标签: python pandas pandas-groupby