警告类型:A value is trying to be set on a copy of a slice from a DataFrame

出错代码:

data['销量'][(data['销量'] < 400) | (data['销量'] > 5000)] = np.nan  # 过滤异常值,将其变为空值
print(data.isnull().sum())

虽然能跑出结果,但是有警告内容:

F:\PycharmProject\13.python_practice_of_data_analysis_and_mining-master\python_practice_of_data_analysis_and_mining-master\chapter4\demo\code\4-1_lagrange_newton_interp.py:12: 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
  data['销量'][(data['销量'] < 400) | (data['销量'] > 5000)] = np.nan  # 过滤异常值,将其变为空值

主要警告的错误为:

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

这里错误的主要原因是我们的索引出现问题,可以改为:

rule=((data.loc[:,'销量'] < 400) | (data.loc[:,'销量'] > 5000))
data.loc[rule,'销量']=np.nan

我们的结果如下,不再报错!

日期    0
销量    6
dtype: int64

你可能感兴趣的:(错误或者警告类型解决办法总结,pandas)