我希望能够用人们的年龄(其中也包含数字值,而不仅仅是NaN值)替换一个列中的缺失值,但到目前为止,我尝试过的所有方法都没有按照我的意愿工作或者完全无法工作。
我希望使用一个遵循正态分布的随机变量生成器,该生成器使用从该列获得的均值和标准差。
我已经尝试了以下方法:
Replacing with numpy, replaces NaN values but with the same number for all of them
df_travel['Age'] = df_travel['Age'].replace(np.nan, round(rd.normalvariate(age_mean, age_std),0))
Fillna with pandas, also replaces NaN values but with the same number for all of them
df_travel['Age'] = df_travel['Age'].fillna(round(rd.normalvariate(age_mean, age_std),0))
Applying a function on the dataframe with pandas, replaces NaN values but also changes all existing numerical values (I only wish to fill the NaN values)
df_travel['Age'] = df_travel['Age'].where(df_travel['Age'].isnull() == True).apply(lambda v: round(rd.normalvariate(age_mean, age_std),0))
希望能得到您的意见。提前感谢。
41.000000 49.000000 37.000000 33.000000 -0.535669 32.000000 59.000000 30.000000 38.000000 36.000000 35.000000 0.041326 31.000000 34.000000 28.000000 29.000000 32.000000 22.000000 53.000000 1.042513
- AWDn0n