简体   繁体   English

熊猫 - 创建一个新专栏

[英]Pandas - Creating a New Column

I have always made new columns in pandas using the following: 我一直在使用以下内容在pandas中创建新列:

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? 在不创建副本的情况下创建新列的方法是什么?

Try using 尝试使用

df.loc[:,'new column'] = value

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 . 作为piRSquared的注释, df可能是另一个DataFrame的副本,当你将值设置为df它可能会在所谓的链索引中产生 Refer to pandas docs for further information. 有关详细信息,请参阅pandas文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM