In this article we will discuss how to add columns in a dataframe using both operator [] and df.assign().

Let’s create a Dataframe object i.e.

Contents of the dataframe dfobj are,

Now lets discuss different ways to add columns in this data frame.

Add new columns in a DataFrame using [] operator

Add a new column with values in list

Suppose we want to add a new column ‘Marks’ with default values from a list. Let’s see how to do this,

As dataframe dfObj didn’t had any column with name ‘Marks’ , so it will add a new column in this dataframe. Updated contents of the dataframe dfobj are,

But we need to keep these things in mind i.e.

  • If values provided in list are less than number of indexes then it will give Value Error.
  • If Column already exists then it will replace all its values.

Add a new column in DataFrame with same default value

Now add a new column ‘Total’ with same value 50 in each index i.e

It will add the new column ‘Total’ and set value 50 at each index in that column. Updated contents of the dataframe dfobj are,

Add a new column in DataFrame with values based on other columns

Let’s add a new column ‘Percentage’ where entry at each index will be calculated by the values in other columns at that index i.e.

It will add the new column ‘Percentage’ , where each entry will contain the percentage of that student, which was calculated based on Marks & Total column values for that index.

Updated contents of the dataframe dfobj are,

Add new columns in a dataFrame using Dataframe.assign()

Pandas Library provides a function to add columns i.e.

It accepts a keyword & value pairs, where a keyword is column name and value is either list / series or a callable entry.
It returns a new dataframe and doesn’t modify the current dataframe.

Let’s add columns in DataFrame using assign() i.e.

Add a column in DataFrame using assign()

Contents of dataframe dfobj are,

Let’s add a column ‘Marks’ i.e.

It will return a new dataframe with a new column ‘Marks’ in that Dataframe. Values provided in list will used as column values. Contents of new dataframe modDf are,

Add multiple columns in DataFrame using assign()

We can also add multiple columns using assign() i.e.

It will add both column Marks & Total. Contents of the returned dataframe are,

Add a columns in DataFrame based on other column

Add column ‘Percentage’ in dataframe, it’s each value will be calculated based on other columns in each row i.e.

Contents of the returned dataframe are,

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.