Reputation: 943
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?
Upvotes: 7
Views: 14224
Reputation: 678
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.
Upvotes: 16