Insert a new column in existing DataFrame

A pandas dataframe is implemented as an ordered dict of columns. In this article, you will get to know how to add a new column to an existing data frame. So first let's create a data frame with values.

Using [] accessor

Here, when you use the [] to pandas DataFrame is quietly performing an outer join or outer merge using the index of the left hand dataframe and the index of the right hand series. df['column'] = series.

Using insert() method

You can inset a column to pandas DataFrmae at a specified index using insert() method.

Here you can see the column 'Age' inserted at the index position of 1 using insert() method.

Using assign() method

Pandas DataFrame has an assign() function that allows you to create a new dataframe with new column added to the old dataframe.
 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About