将新列添加到数据框-SettingWithCopyWarning

时间:2020-05-06 17:34:28

标签: python pandas dataframe

我有一个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

我做错什么了吗?

0 个答案:

没有答案