Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filling mean age according the class of the Student

I have a df with students from three different classes. I am trying to fill in the missing ages based on the mean age of the other students in the same class. I tried two different ways. One is working and the other one is not . I am not able to figure out why that is the case as I feel both ways are doing the exact same thing. Could you kindly explain me why the solution B is not working while A works?

Solution A: (Working)

df.loc[(df['Age'].isna()) & (df['Class'] == 1),'Age'] = mean_age

Solution B: (not working)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.loc[df['Class'] == 1,'Age'].fillna(mean_age, inplace=True)

>Solution :

IIUC:

df['Age'] = df['Age'].fillna(df.groupby('Class')['Age'].transform('mean'))

The solution B can’t work because you slice your dataframe so you create a "copy" and fill nan values inplace. The copy is filled but not the original dataframe.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading