Pandas Add Column with Default Values

The Pandas software package for the Python programming language allows for the manipulation and analysis of data. It provides particular methods and data structures for dealing with the mathematical tables and time series. We can enter the columns in Pandas with the default values. The different methods for inserting the columns with the default values are provided by Pandas. In this tutorial, we will investigate the methods for adding the default values to the columns in Pandas and put a default value into a column.

Methods for Adding Columns with Default Values in Pandas

In this tutorial, we will add the column’s constant value, utilizing the three different methods. These are what they are:

As we move toward the examples, we employ each of these approaches separately. We investigate each of the three methods in great detail in this tutorial by applying them to our codes.




DataFrame.insert(loc, column, value, allow_duplicates=False)

Example 1: Pandas.DataFrame.assign() Method

We move to generate the “Pandas” code. For this, we utilize the “Spyder” tool. Here, you can notice that we “import” the “Pandas as pd” at the start of the code. Now, we have the “Utensils” and put some in it as we set the column name as “items” and place the “Glass”, “Cups”, “Plates”, “Teapot”, and “Spoon” in it. After this, we have a column named “Rate” and add the price here as “1000”, “1500”, “2600”, “900”, and “500”. We also add another column name which is “Quantity”.

Now, we convert this “Utensils” data into a “DataFrame”. We initialize the “Utensils1” with the “pd.DataFrame” and put the “Utensils” in its brackets to convert them into a DataFrame. Now, we print this “Utensils1” DataFrame. In this DataFrame, we did not add any default value column yet, so we move towards the method 1 which is the “Pandas.DataFrame.assign()” method. Before this, we print two lines which is rendered on the terminal. Then, we add this “Pandas.DataFrame.assign()” method. We initialize another variable named “Utensils2” with the “Pandas.DataFrame.assign()” method and put the previous DataFrame “Utensils1”. Then, we add a column named “Profit”. Its default value is set to “NAN”. Now, to render the DataFrame “Utensils2”, we have to print “Utensils2”.

We get this output by simply pressing the “Shift+Enter” or you may use the “run” button. Notice that there are three columns here. The first and all columns and rows contain different values. But in the following illustration, we have four columns and the last “Profit” column contains the default value which is “NAN” in this example.

Example 2: Utilizing [ ] Operator

In this example, we will use another method to add the new column with the default value. We start our code by importing the “pandas as pd” and create some data again. We use the variable name “Courses” and add three different columns to it. First, we have the “Subject” in which we add 5 subjects named “English”, “Computer”, “Mathematics”, “Physics”, and “Chemistry”. Then, we utilize the “Fee” as the next column name. In this column, we add some amount as “1000”, “1500”, “2000”, “1800”, and “1900”. After this, we create the last column with the name “Duration”. In this column, we add “10 days”, “15 days”, “12 days”, “20 days”, and “18 days”.

Now, alter them into a DataFrame by utilizing the “pd.DataFrame” and save it in the “Courses1” and print the “Courses1”. Also, print the two lines below this DataFrame. Now, we move to utilize the second method to add the column with the default value. We put the name of the DataFrame and then place the square bracket. In this square bracket, we place the name of the column that we want to add as we write it as “Course1[‘Discount’]”. Here, we first add the “Discount” column and put its default value as “NAN”. Then, we place the “Discount_percentage” below. Its default value is set to “0%”. To render this DataFrame, we utilize the “print()” method and place the “Courses1” in it.

The three columns are here in the first DataFrame. Each column and each row have a separate value. However, in the next DataFrame, the last two columns, “Discount” and “Discount_percentage” columns, have the default value which, in this case, are “NAN” and “0%”, respectively. There are five columns total.

Example 3: Pandas.DataFrame.insert() Method

Here is the code in which we utilize the third method which is the “Pandas.DataFrame.insert()” method. We must import the “pandas as pd” and then do the remaining code. We generate the “Employees” and add three columns named “Employee”, “Pay”, and “Working_Hours”. We also put some information in it. In the “Employee” column, we have “John”, “George”, “Micheal”, “Henry”, and “Cherry”. Below it, we have the “Pay” column in which we add the “15000”, “18000”, “20000”, “18500”, and “19700”. We also have the “Working_Hours” column and in this, we add the “5hrs/day”, “6hrs/day”, “8hrs/day”, and “5.5hrs/day”, and “6.5hrs/day”, respectively. Then, we alter this data into the DataFrame named “Employee1” and print the “Employee1” DataFrame.

We utilize the third method which is the “Pandas.DataFrame.insert()” method. We place the position where we insert the new column which is “2”. The name of this column is “Payment”. We add the default value as “Monthly”. Then, “False” the “allow_duplication” in it. After this, we render this new DataFrame by utilizing the “print()” function.

Here is the outcome of this code. You can easily observe the difference between the first DataFrame and the second DataFrame.

Example 4: Utilize All Three Methods

Now, we move to our last code here. In this example, we utilize all the three methods to add the column with the default value. After importing the “pandas as pd”, we use the “Data” variable and place the three columns with the names “Fruits”, “Vegetables”, and “Dry Fruits”. Also, add some fruits names, vegetable names, and dry fruits names in them and convert them into a DataFrame.

Now, we utilize the “Pandas.DataFrame.assign()” method first to add the “Column” with the default value as “Default”. Then, we utilize the “Pandas.DataFrame.insert()” method and add the “2nd Column” in the “3” position and set its default value as “Default value”. We also utilize the third method by using the “[ ] operator” in this added column named “3rd Column” with the default value “NAN”. Then, print this DataFrame where we added the three columns with the default values.

The result is presented here and all the three columns are added in the following DataFrame with the default value:


In this tutorial, the methods to add the column with the default value are thoroughly discussed in a very simple way. The main objective of this tutorial is to help you know the idea of “adding a column with the default value” in Pandas. We discussed the three methods in this tutorial for adding a column with a constant value. These are rather simple methods to add the columns in Pandas with a default value.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.