更改 pandas 列中的值:设置与复制警告:正在尝试在数据帧错误的切片副本上设置值



所以我有这个熊猫df命名为matches,它有两列"线程"和"当前电子邮件"。"当前电子邮件"是从另一个名为df 的 df复制的:

matches['Current email'] = df['description']

线程只有两个值:线程和非线程因此,如果电子邮件是一个线程,我想将具有相同索引的"当前电子邮件"值更改为 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: 正在尝试在数据帧中的切片副本上设置值

我尝试了这样的事情:

matches['Current email'] = df['description'].copy()

更糟糕的是,相同的代码适用于其他数据。

有什么帮助吗?

您可以使用布尔索引:

filter_thread = matches['Thread'] != 'not thread'
matches.loc[filter_thread, 'Current email'] = 8

最新更新