如何通过添加因子(没有SettingWithCopyWarning)正确更新数据框列中的值?

时间:2019-04-03 16:16:24

标签: python pandas dataframe

我想在数据框中的一列中更新值(有条件)。没有SettingWithCopyWarning怎么办?

我使用.loc函数完成此操作,但是无论如何我都会得到SettingWithCopyWarning

我尝试使用以下代码

    df.loc[df.nHeight < 1450, col_name] = df.nHeight + 145

    df.loc[df['nHeight'] < 1450, 'nHeight'] = df['nHeight'] + 145

在两种情况下(以及其他情况下),我都会收到相同的警告:

lib/python3.6/site-packages/pandas/core/indexing.py:543: 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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

有什么想法在这种情况下如何删除此警告?

0 个答案:

没有答案