Pandas - 创建一个新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41274332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas - Creating a New Column
提问by Jeff Saltfist
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?
在不创建副本的情况下创建新列的方法是什么?
回答by Miquel
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. Refer to pandas docsfor further information.
作为 piRSquared 评论,df
可能是另一个 DataFrame 的副本,当您为其设置值时df
,可能会导致所谓的链索引。有关更多信息,请参阅Pandas 文档。