完整警告为: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().drop(
解决方法
把原来类似这样的代码:
dataframe.drop('删除列', axis=1, inplace=True)
删除inplace=True
,同时赋给新的变量,修改后的代码为:
df_new = dataframe.drop('删除列', axis=1)
问题解析
这样的操作时没有问题的:
import pandas as pd
df = pd.DataFrame([[1, 2, 3],
[2, 3, 4]])
df.drop(1, axis=1, inplace=True)
但是如果是对切片的dataframe做drop()
操作同时还指定inplace=True
,就会警告:
import pandas as pd
df = pd.DataFrame([[1, 2, 3],
[2, 3, 4]])
df[:1].drop(1, axis=1, inplace=True)
而查看df
变量发现它其实没有变化