在情况1中,df[’A’]创建的副本df。正如Pandas文档所解释的那样,这在链接时可能导致意外结果,从而引发警告。情况2看起来正确,但可能会出现误报:
警告:链接的作业警告/异常旨在将可能无效的作业通知用户。可能存在误报;意外报告链接分配的情况。
要关闭SettingWithcopyWarning单个数据框,请使用
df.is_copy = False
要完全关闭链接分配警告,请使用
options.mode.chained_assignment = None解决方法
我得到的SettingWithCopyWarning,我不希望他们在案件的错误:
N.In <38>: # Column B does not exist yetN.In <39>: df[’B’] = df[’A’]/25N.In <40>: df[’B’] = df[’A’]/50/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/indexing.py:389: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.Try using .loc[row_index,col_indexer] = value instead self.obj[item] = s
和
N.In <41>: df.loc[:,’B’] = df[’A’]/50/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/indexing.py:389: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.Try using .loc[row_index,col_indexer] = value instead self.obj[item] = s
为什么情况1和情况2会发生这种情况?