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 Sector
and Price
:
