How to replace NaN values in Pandas conditionally?

前端 未结 1 1234
梦谈多话
梦谈多话 2021-01-27 21:07

I\'m using famous Titanic dataset for my first Kaggle problem. I\'m getting stuck in dataset. I want to replace NaN values of Age gender wise e.g. missing values for \'male\' sh

相关标签:
1条回答
  • 2021-01-27 21:48
    import pandas as pd
    import numpy as np
    
    df = pd.read_csv('train.csv')
    df['Age'].fillna(df.groupby(["Sex"])["Age"].transform(np.mean), inplace=True)
    

    Maybe this was something you were trying to do? I didn't get any warning though. Have a look at my blog post too if necessary.

    0 讨论(0)
提交回复
热议问题