按比例填充na
我正在尝试使用以下代码按比例填充缺失值,但此代码不起作用,有人可以帮助我吗?
单击上面查看
测试缺失值的
df['Diarrhoea'].value_counts(normalize=True, dropna=False)
output:
0.0 0.686439
1.0 0.238198
NaN 0.075363
Name: Diarrhoea, dtype: float64
df['Diarrhoea'].value_counts(normalize=True)
Output:
0.0 0.742388
1.0 0.257612
Name: Diarrhoea, dtype: float64
df['Diarrhoea'] = df['Diarrhoea'].fillna(pd.Series(np.random.choice(['0.0', '1.0'],
p=[0.75,0.25], size=len(df))))
屏幕截图现在,如果我运行以下命令,它会显示 NaN 值:
df[df['Diarrhoea'].isnull()]
I am trying the following code to fill missing values by proportion but this code isnt working, is there someone who can help me?
Click above to see screenshot
testing missing values
df['Diarrhoea'].value_counts(normalize=True, dropna=False)
output:
0.0 0.686439
1.0 0.238198
NaN 0.075363
Name: Diarrhoea, dtype: float64
df['Diarrhoea'].value_counts(normalize=True)
Output:
0.0 0.742388
1.0 0.257612
Name: Diarrhoea, dtype: float64
df['Diarrhoea'] = df['Diarrhoea'].fillna(pd.Series(np.random.choice(['0.0', '1.0'],
p=[0.75,0.25], size=len(df))))
Now if i run the following command it shows me NaN values:
df[df['Diarrhoea'].isnull()]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是索引对齐/不匹配。您正在尝试使用范围索引与数据帧索引不同的系列来填充缺失值,因此没有匹配,因此缺失值不会被填充。
解决方案是将数据帧的索引分配给您创建的系列以填充缺失值:
The problem here is index alignment/mismatch. You are trying to fill the missing values using a series which has a range index different from the index of your dataframe so there is no match and hence missing values are not filled.
The solution is to assign the index of dataframe to the series that you have created to fill the missing values: