Python 类型错误:';浮动';对象不可下标(尝试在数据帧上应用函数时)
我正在尝试在熊猫数据帧上应用此函数。但我得到了这个错误。我想知道这是什么意思,以及如何纠正它Python 类型错误:';浮动';对象不可下标(尝试在数据帧上应用函数时),python,pandas,Python,Pandas,我正在尝试在熊猫数据帧上应用此函数。但我得到了这个错误。我想知道这是什么意思,以及如何纠正它 def fill_age(x): Age = x['Age'] Pclass = x['Pclass'] Sex = x['Sex'] if pd.isnull(Age): if Pclass == 1: return 34.61 + np.random.normal(loc =0, scale = 13.61)
def fill_age(x):
Age = x['Age']
Pclass = x['Pclass']
Sex = x['Sex']
if pd.isnull(Age):
if Pclass == 1:
return 34.61 + np.random.normal(loc =0, scale = 13.61)
elif (Pclass == 1) & (Sex == 'male'):
return 41.2813 + np.random.normal(loc = 0, scale = 15.14)
elif (Pclass == 2) & (Sex == 'female'):
return 28.72 + np.random.normal(loc = 0, scale = 12.87)
elif (Pclass == 2) & (Sex == 'male'):
return 30.74 + np.random.normal(loc = 0, scale= 14.79)
elif (Pclass == 3) & (Sex == 'female'):
return 21.75 + np.random.normal(loc = 0, scale = 12.73)
elif (Pclass == 3) & (Sex == 'male'):
return 26.51 + np.random.normal(loc = 0, scale= 12.16)
else:
pass
else:
return Age
train['Age'] = train['Age'].apply(fill_age)
注:列车是一个数据帧
您不显示以下位置:
train['Age']=train['Age']。申请(填写年龄)
来自,但我怀疑“train”实际上是一个float而不是dict。完整的回溯会很有用。