O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding new columns with [] and .insert()

New columns can be added to a data frame using the [] operator. Let's add a new column named RoundedPrice which will represent the rounding of values in the Price column.

The way that pandas performs this is by first selecting the Price column's data from sp500 and then all of the values in that Series are rounded. Then pandas aligns this new Series with the copy DataFrame and adds it as a new column named RoundedPrice. The new column is added to the end of the columns index.

The .insert() method can be used to add a new column at a specific location. The following inserts the RoundedPrice column between ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required