python - 向数据框添加新列 - 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
难道我做错了什么?
解决方案
推荐阅读
- java - Dagger:在为 Kotlin 嵌套类生成的组件代码中找不到符号错误
- php - 如何将查询构建器代码转换为 laravel eloquent(获得更快的大量数据)?
- python - 添加新元素时Heapify不起作用
- javascript - 使 HTML 元素沿特定滚动条移动,而不是主滚动条
- python - Windows 上的 Python 子进程
- c - C语言中的父进程、子进程和孙进程之间的通信
- java - 是否可以使用 Java 8 Streams 过滤器访问列表的索引?
- html - 在 sweetalert2 内显示不显示和块的表
- sql - Oracle 数据库 10G 查询待解决
- php - Laravel:用户 15 天未登录系统然后发送邮件