我试图应用这个函数来填充基于Pclass
和Sex
列的Age
列。但我不能这么做。我怎样才能让它工作
def fill_age():
Age = train['Age']
Pclass = train['Pclass']
Sex = train['Sex']
if pd.isnull(Age):
if Pclass == 1:
return 34.61
elif (Pclass == 1) and (Sex == 'male'):
return 41.2813
elif (Pclass == 2) and (Sex == 'female'):
return 28.72
elif (Pclass == 2) and (Sex == 'male'):
return 30.74
elif (Pclass == 3) and (Sex == 'female'):
return 21.75
elif (Pclass == 3) and (Sex == 'male'):
return 26.51
else:
pass
else:
return Age
train['Age'] = train['Age'].apply(fill_age(),axis=1)
我得到以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
你应该考虑使用括号来分离参数(你已经做过了),并改变布尔运算符^ {< CD1>},以获得位类型opeActer ^ {CD2>},以避免这种类型的错误。此外,请记住,如果要使用
apply
,则应为函数使用参数x
,该函数将成为apply
函数中lambda的一部分:现在,对lambda使用apply:
在示例数据帧中:
使用上面提供的答案:
输出:
相关问题 更多 >
编程相关推荐