Pandas: filling missing values by mean in each group

前端 未结 9 995
耶瑟儿~
耶瑟儿~ 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:45

    The featured high ranked answer only works for a pandas Dataframe with only two columns. If you have a more columns case use instead:

    df['Crude_Birth_rate'] = df.groupby("continent").Crude_Birth_rate.transform(
        lambda x: x.fillna(x.mean()))
    

提交回复
热议问题