我正在尝试为数据框中的user_score列填充缺少的值。数据当前为字符串,包括'tbd'
。我想用NaN
替换'tbd'
值,然后将列转换为float,然后计算游戏类型的user_score
平均值,并根据类型将此平均值应用于user_score
列中的每个NaN值(而不是使用总体用户得分平均值)
games['user_score'] = games['user_score'].replace('tbd', np.nan, inplace=True)
games['user_score'] = games['user_score'].astype(float)
#genre_mean = games.groupby('genre').agg({'user_score':np.mean})
games['user_score'] = games.groupby('genre', sort=False)['user_score'].apply(lambda x: x.fillna(x.mean()))
print(games.groupby('genre').agg({'user_score':np.mean}))
print(games['user_score'].head(10))
现在,当我在代码末尾打印groupby时,它会显示每个流派的用户_分数的平均值,即每个流派的NaN。然后我尝试在apply函数中使用.nanmean()
,但它给了我一个错误。如何根据游戏类型将“用户分数”平均值应用于“用户分数”列中缺失的值
谢谢大家!
第一行中的
user_score
正在变为null:如果同时分配
user_score
和使用inplace=True
,则会清除user_score
不带
inplace
的赋值返回:或使用
inplace
而不重新分配:相关问题 更多 >
编程相关推荐