我相信你
df
是另一个的副本
pandas.DataFrame
.
以下复制了
SettingWithCopyWarning
.
import pandas as pd
raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]]
df.drop(["a"], 1, inplace = True)
设置为复制警告:
试图在数据帧切片的副本上设置值
请参阅文档中的注意事项:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
错误=错误)
当你再做一个
熊猫.DataFrame
从现有的,从来没有做直接分配像
df = raw[["a", "b"]]
. 相反,使用
pandas.DataFrame.copy()
.
raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]].copy()
df.drop(["a"], 1, inplace = True)
现在警告消失了,因为
数据框
是一个新的对象,因此根据
the official document
:
对副本数据或索引的修改不会反映在原始对象中