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?
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 docs for further information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.