【问题标题】:Python: replace NULLs with AveragePython:用平均值替换 NULL
【发布时间】:2020-12-08 10:48:05
【问题描述】:

我是 Python 新手,需要您的专家帮助。 我有一个多行的表格,用于相同的游戏。但是在“评级”行中,这个或那个游戏的一些单元格是空的,有些则不是。 问题:如何用同一游戏的平均评分填充列中的缺失值?

请看下图中的表结构:

enter image description here

提前谢谢你!!

【问题讨论】:

    标签: python null mean fillna


    【解决方案1】:

    假设您要使用 'Name' 中组的平均值来填充值,您可以使用 pandas groupbypandas applypandas fillna 的组合

    >>>df['Rating_score']=df.groupby('Name')['Rating_score'].apply(lambda x:x.fillna(x.mean()))
    >>>df
      
        Name     Genere  Rating_score
    0  Game1  Adventure          65.0
    1  Game1  Adventure          70.0
    2  Game1  Adventure          67.5
    3  Game2     Racing          59.0
    4  Game2     Racing          61.5
    5  Game2     Racing          64.0
    6  Game3   Shooting          71.0
    7  Game3   Shooting          71.0
    

    【讨论】:

    • 你好 Shijith!感谢您的帮助,您的方法很有魅力。祝你有美好的一天:)
    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2013-04-01
    • 2019-03-21
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多