Pandas: filling missing values by mean in each group

前端 未结 9 962
耶瑟儿~
耶瑟儿~ 2020-11-22 06:06

This should be straightforward, but the closest thing I\'ve found is this post: pandas: Filling missing values within a group, and I still can\'t solve my problem....

<
相关标签:
9条回答
  • 2020-11-22 06:54
    df.fillna(df.groupby(['name'], as_index=False).mean(), inplace=True)
    
    0 讨论(0)
  • 2020-11-22 06:54

    You can also use "dataframe or table_name".apply(lambda x: x.fillna(x.mean())).

    0 讨论(0)
  • 2020-11-22 07:03

    I'd do it this way

    df.loc[df.value.isnull(), 'value'] = df.groupby('group').value.transform('mean')
    
    0 讨论(0)
提交回复
热议问题