python - settingwithcopywarning Pandas 同时对我的数据框使用 .remove() 方法
问题描述
所以,我只是想用列值的平均值替换我的数据框中的所有 nan 值,我已经使用了这个.remove()
函数,它在那里弹出错误
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
return super().replace(
我浏览了互联网,发现我们可以使用该iloc
方法,或者loc
我不明白在这种情况下如何使用它,即在函数中修改值。
这是我写的代码
avghorse=df[["horsepower"]].astype(float).mean(axis=0)
df[["horsepower"]].replace(np.NaN,avghorse,inplace=True)
avghorse 是列的平均值。该列的数据类型是浮点数。
而且,我已将 coulms 数据类型转换为浮点数,当我使用该df[["horsepower"]].astype(float)
行时,它仍然向我显示它是一个对象。
我正在使用 jupyterlab 和 python 3.9.5
解决方案
推荐阅读
- bash - 带有运行另一个脚本的选项的 Bash 脚本
- continuous-integration - 根据分支提交 yml 解析错误,Appveyor 部署到 2 个不同的位置
- html - HTML标签在 IE/Edge 中不起作用
- java - Java - 期货不取消
- javascript - 幻灯片可见时启动计时器reveal.js
- excel - 如何根据单元格背景颜色删除行?
- r - 表示以 t 为底的双精度所需的位数(以 t 为底)?
- android - JobScheduler 每 15 分钟执行一次
- javascript - 关闭切换时“返回”内容
- webpack - 升级到 vue-cli 3 后,HTML 中的空格消失了