首页 > 解决方案 > Python Pandas 在 csv 文件警告消息中添加新列

问题描述

我正在尝试在 CSV 文件中添加新列。

当我使用时df3['New header'] = None,出现以下错误

`/Users/seven/PycharmProjects/Python3/venv/lib/python3.7/site-packages/pandas/core/indexing.py:966: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[item] = s

所以我变成了df3.loc[index, 'New header'] = None

但是我又遇到了同样的错误。

有趣的部分是两种方式都成功添加了新列。

我怎样才能摆脱错误信息?

谢谢

标签: pythonpython-3.xpandas

解决方案


推荐阅读