Pandas: How to fill null values with mean of a groupby?

后端 未结 2 1284
长发绾君心
长发绾君心 2020-12-10 14:47

I have a dataset will some missing data that looks like this:

id    category     value
1     A            NaN
2     B            NaN
3     A            10.5
         


        
2条回答
  •  生来不讨喜
    2020-12-10 15:23

    I think you can use groupby and apply fillna with mean. Then get NaN if some category has only NaN values, so use mean of all values of column for filling NaN:

    df.value = df.groupby('category')['value'].apply(lambda x: x.fillna(x.mean()))
    df.value = df.value.fillna(df.value.mean())
    print (df)
       id category  value
    0   1        A   6.25
    1   2        B   1.00
    2   3        A  10.50
    3   4        C   4.15
    4   5        A   2.00
    5   6        B   1.00
    

提交回复
热议问题