首页 > 解决方案 > 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

标签: pythonpandasdataframe

解决方案


推荐阅读