SettingWithCopyWarning:A value is trying to be set on a copy of a slice from a DataFrame

版权声明:转载请注明作者(独孤尚良dugushangliang)出处:https://blog.csdn.net/dugushangliang/article/details/125235598

进行pandas的Dataframe数据操作时,报错如下:

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

这个涉及到的是pandas的Dataframe数据读写中的数据复制问题。

SettingWithCopyWarning:A value is trying to be set on a copy of a slice from a DataFrame_第1张图片

因为涉及到写入的操作,如果只是读取没有这个问题。那如何避免这种情况呢?

ustar=df['u']

df['u'][ustar==-9999]=None

df.loc[ustar==-9999,'u']=None

用loc函数修改Dataframe即可,如上面末行代码所示。

独孤尚良dugushangliang——著

你可能感兴趣的:(#,Pandas,pandas,DataFrame,CopyWarning,loc)