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

后端 未结 2 1279
长发绾君心
长发绾君心 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
    
    0 讨论(0)
  • 2020-12-10 15:29

    You can also use GroupBy + transform to fill NaN values with groupwise means. This method avoids inefficient apply + lambda. For example:

    df['value'] = df['value'].fillna(df.groupby('category')['value'].transform('mean'))
    df['value'] = df['value'].fillna(df['value'].mean())
    
    0 讨论(0)
提交回复
热议问题