• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

即使使用loc(?),也可以使用SettingWithCopyWarning

用户头像
it1352
帮助1

问题说明

在我不希望出现的情况下,我得到SettingWithCopyWarning错误:

I get SettingWithCopyWarning errors in cases where I would not expect them:

N.In <38>: # Column B does not exist yet
N.In <39>: df['B'] = df['A']/25
N.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中会发生这种情况?

Why does it happen in case 1 and 2?

正确答案

#1

在情况1中,df['A']创建了df的副本.正如 Pandas文档所述,链接时可能会导致意外结果,从而引发警告.情况2看起来正确,但可能会出现误报:

In case 1, df['A'] creates a copy of df. As explained by the Pandas documentation, this can lead to unexpected results when chaining, thus a warning is raised. Case 2 looks correct, but false positives are possible:

警告:链接的分配警告/异常旨在 通知用户可能无效的分配.可能有假 肯定;链式作业无意间发生的情况 报告.

Warning: The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertantly reported.

要为单个数据框关闭SettingWithCopyWarning,请使用

To turn off SettingWithCopyWarning for a single dataframe, use

df.is_copy = False

要完全关闭链接的分配警告,请使用

To turn off chained assignment warnings altogether, use

options.mode.chained_assignment = None

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkhaa
系列文章
更多 icon
同类精品
更多 icon
继续加载