python - 更改熊猫列中的值:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame error
问题描述
所以我有这个名为matches的 pandas df ,它有两列“线程”和“当前电子邮件”。“当前电子邮件”是从另一个名为df的 df复制的:
matches['Current email'] = df['description']
Thread 只有两个值:thread和not thread。因此,如果电子邮件是一个线程,我想将具有相同索引的“当前电子邮件”值更改为 8(实际上是另一件事,但为了便于理解 8)。
for index,value in matches['Thread'].iteritems():
if value=='not thread':
pass
else:
matches.iloc[index]['Current email'] = 8
运行此警告后/usr/local/envs/py3env/lib/python3.5/site-packages/ipykernel/__main__.py:5: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame过来。
我试过这样的事情:
matches['Current email'] = df['description'].copy()
更奇怪的是,相同的代码也适用于其他数据。
有什么帮助吗?
解决方案
您可以使用布尔索引:
filter_thread = matches['Thread'] != 'not thread'
matches.loc[filter_thread, 'Current email'] = 8
推荐阅读
- perl - 如何在 Perl 中将总线一元化和/减少一点?
- python - 如何在python Tkinter程序中将变量参数传递给MYSQL数据库查询
- java - Springboot 应用程序已启动,但 POST 请求始终返回“未找到”
- bash - find 列出的文件列表的 md5sum
- angular - 如何在 *ngfor 项目中单击按钮时显示单独的事件?
- jenkins - 如何在管道的下游作业中获取上游作业已运行的节点名称
- ios - 了解 Apple 远程收据
- javascript - 验证完成后,Nestjs 将函数应用于每个控制器函数/路由
- vulkan - 为什么有些命令只能在渲染通道之外记录?
- keras - (深度学习,rnn,cnn)图像图像 captinoing 与 keras