【问题标题】:pandas SettingWithCopyWarning after trying .loc尝试 .loc 后的熊猫 SettingWithCopyWarning
【发布时间】:2016-07-03 19:41:41
【问题描述】:

首先,我构建了一个新的 DataFrame 框架。然后通过从框架中过滤一些数据来创建一个新的框架2​​。现在我想给 frame2 赋值:

import numpy as np
from pandas import DataFrame

frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'], columns=['Ohio', 'Texas', 'California'])
mask = frame['Texas'] > 1
print frame[mask]
frame2 = frame.loc[mask]
frame2.loc['c', 'Ohio'] = 'me'
print frame2

但我收到了这个警告:

C:\Python27\lib\site-packages\pandas\core\indexing.py:461: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

尽管我使用了推荐的 .loc 语法,为什么我仍然收到此警告?我应该怎么做才能避免这个警告?

【问题讨论】:

  • 看来我在stackoverflow.com/questions/23688307/… 找到了一种可能的解决方案。有人有其他好主意吗?
  • 你在这里的意图是什么?您要更新副本还是原始 df
  • @EdChum 我想更新副本。

标签: python pandas


【解决方案1】:

变化

frame2 = frame.loc[mask]

frame2 = frame.loc[mask].copy()

消除此警告。

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2018-09-11
    • 2018-12-22
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2014-05-25
    相关资源
    最近更新 更多