Pandas: filling missing values by mean in each group

前端 未结 9 1087
耶瑟儿~
耶瑟儿~ 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 07:03

    I'd do it this way

    df.loc[df.value.isnull(), 'value'] = df.groupby('group').value.transform('mean')
    

提交回复
热议问题