我有一个 df,其中有一列 Critic_Score,该列具有 NaN 值。我试图用同一平台的评论家分数的平均值来替换它们。这个问题已经在堆栈溢出上被问过好几次了,我使用了 4 个建议,但没有给我想要的输出。请告诉我如何解决这个问题。这是 df 的子集:x[['Platform','Critic_Score']].head()Platform Critic_Score0 wii 76.01 nes NaN2 wii 82.03 wii 80.04 gb NaN有关原始 df 的更多信息:x.head().to_dict('list'){'Name': ['wii sports', 'super mario bros.', 'mario kart wii', 'wii sports resort', 'pokemon red/pokemon blue'], 'Platform': ['wii', 'nes', 'wii', 'wii', 'gb'], 'Year_of_Release': [2006.0, 1985.0, 2008.0, 2009.0, 1996.0], 'Genre': ['sports', 'platform', 'racing', 'sports', 'role-playing'], 'NA_sales': [41.36, 29.08, 15.68, 15.61, 11.27], 'EU_sales': [28.96, 3.58, 12.76, 10.93, 8.89], 'JP_sales': [3.77, 6.81, 3.79, 3.28, 10.22], 'Other_sales': [8.45, 0.77, 3.29, 2.95, 1.0], 'Critic_Score': [76.0, nan, 82.0, 80.0, nan], 'User_Score': ['8', nan, '8.3', '8', nan], 'Rating': ['E', nan, 'E', 'E', nan]}这些是我尝试过的语句及其输出:1.x['Critic_Score'] = x['Critic_Score'].fillna(x.groupby('Platform')['Critic_Score'].transform('mean'), inplace = True)0 None1 None2 None3 None4 NoneName: Critic_Score, dtype: objectx.loc[x.Critic_Score.isnull(), 'Critic_Score'] = x.groupby('Platform').Critic_Score.transform('mean')#no change in column0 76.01 NaN2 82.03 80.04 NaNx['Critic_Score'] = x.groupby('Platform')['Critic_Score']\ .transform(lambda y: y.fillna(y.mean()))#no change in column0 76.01 NaN2 82.03 80.04 NaNName: Critic_Score, dtype: float64x['Critic_Score']=x.groupby('Platform')['Critic_Score'].apply(lambda y:y.fillna(y.mean()))x['Critic_Score'].head()Out[73]:0 76.01 NaN2 82.03 80.04 NaNName: Critic_Score, dtype: float64
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
x.update( x.groupby('Platform').Critic_Score.transform('mean'), overwrite=False)
首先,您创建一个具有相同行数但每行具有平台平均值的新 df。
然后用它来更新原来的
请记住,您的样本只有一行nes
和另一行gb
,两者都有nan
分数,所以没有什么可以平均的
添加回答
举报
0/150
提交
取消