0

I am struggling with something that might seem simple: I want to replace NaNby the mean of the value for the Disease

import numpy as np
import pandas as pd

df = pd.DataFrame({'Disease' : [1, 0, 0, 1, 1], 'Value1' : [3, 1, 2, 4, np.nan], 
                   'Value2' : [13, 21, np.nan, 16, 87], 'Value3' : [np.nan, 5, 11, 54, np.nan]})

My data with NaN

I want to replace the NaNvalue by these:

df.groupby(by='Disease').mean()

enter image description here

I tried to loop through the dataframe but I am stuck when I try to assign the new value in the original dataframe.

Can someone help me?

Thank you

onthemoon01
  • 103
  • 1
  • 10
  • 1
    `df.fillna(df.groupby('Disease').transform('mean'))` : transform mean and then fillna – anky Apr 13 '20 at 14:27
  • Although I searched for a while, I could not find the aforementioned answer. Sorry for the repost and thank you for your answer. – onthemoon01 Apr 13 '20 at 19:17

0 Answers0