Jeff Saltfist
Jeff Saltfist

Reputation: 943

Pandas - Creating a New Column

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

Answers (1)

Miquel
Miquel

Reputation: 678

Try using

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

As piRSquared comments, dfis 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

Related Questions