Adding columns using concatenation
Both the []
operator and .insert()
method modify the target data frame in-place. If a new data frame with the additional columns is desired (leaving the original unchanged) then we can use the pd.concat()
function. This function creates a new data frame with all of the specified DataFrame
objects concatenated in the order of specification.
This following creates a new DataFrame
with a single column containing the rounded price. It then uses pd.concat()
with axis=1
to signify that the given DataFrame
objects should be concatenated along the columns axis (as compared to rows which would use axis=0
) .

Note
Concatenation will be covered in more detail in Chapter 11, Combining, Relating, and Reshaping Data.
It is possible to have duplicate column names as a result of a concatenation. To demonstrate this happening, let's recreate rounded_price
but name the column Price
.

The concatenation will now result in duplicate columns.

Interestingly, you can retrieve both columns...