pandas -创建新列 [英] Pandas - Creating a New Column
本文介绍了 pandas -创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我总是使用以下命令在熊猫中添加新列:
I have always made new columns in pandas using the following:
df['new_column'] = value
我正在使用此方法,但是收到设置副本的警告.
I am using this method, however, am receiving the warning for setting a copy.
在不创建副本的情况下创建新列的方法是什么?
What is the way to make a new column without creating a copy?
推荐答案
尝试使用
df.loc[:,'new column'] = value
作为piRSquared注释,df
可能是另一个DataFrame的副本,当您将值设置为df
时,它可能会发生所谓的链式索引.有关更多信息,请参考 pandas文档 .
As piRSquared comments, df
is probably a copy of another DataFrame and when you set values to df
it probably incurs in what is called chain indexing. Refer to pandas docs for further information.
这篇关于 pandas -创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文