首页 > 解决方案 > 向数据框添加新列 - SettingWithCopyWarning

问题描述

我有一个pandas数据框(熊猫版本'0.24.2')和一个长度相同的列表。

我想将此列表作为列添加到数据框中。

我这样做:

df.loc[:, 'new_column'] = pd.Series(my_List, index=df.index)

但我收到此警告:

.../anaconda/lib/python3.7/site-packages/pandas/core/indexing.py:362: 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[key] = _infer_fill_value(value)
.../anaconda/lib/python3.7/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

难道我做错了什么?

标签: pythonpandasdataframe

解决方案


推荐阅读