pandas Add Column With Constant Value

The Python programming language has a software package called pandas to manipulate and analyze data. It includes specific data structures and procedures for working with time series and mathematical tables. In “pandas”, we can insert columns with different and constant values. The “pandas” provides various techniques for inserting columns with a constant value. The constant value is the same in the column. If we put “1” as the constant value, then the value is 1 throughout the column. In this article, we will insert the constant value in a column and explore the methods used for adding constant value in columns in “pandas”.

Methods for Adding Columns in pandas

In this article, we’ll add the constant value of the column using three distinct approaches. They are as follows:

  • pd. Series () method.
  • As a static value method
  • DataFrame.insert() method

We will use these methods separately in each example as we go on to them. We will examine each method in great detail in this guide by applying the two techniques to our codes.

Example 1: Using the DataFrame.series () Method

All of the examples are run through the “Spyder” tool. Before adding specific code to this software utility, we must first install the “Spyder” tool. To quickly access the panda’s methods by simply entering “pd”, we are importing the “pandas as pd” command. We have a DataFrame below this with the name “Machinery”. This DataFrame is created here by utilizing “pd.DataFrame”. We insert some data here, which will render in columns and rows. We insert four columns with unique names, and the names are “Name”, “Quantity A”, “Quantity B”, and “Quantity C”. In the names column, we are adding “Motor”, “Generator”, “Solar”, “UPS”, and “Fan”.

Here, the first column is completed, and we add data to the next column, named “Quantity A”. In this column, we are inserting “1”, “2”, “3”, “4”, and “5”. Now, comes the third column, “Quantity B”, the data which we added in this column is “1”, “4”, “9”, “16”, and “25”. The last column’s data we are adding here is “1”, “8”, “27”, “64”, and “125”.

Now, we print “Initial Machinery Quantity” by putting this line in the “print ()” method. Then, we also render this DataFrame on the terminal with the same method. After this, we want to add a column with a constant value in this existing DataFrame. For this, we are utilizing the “pd. Series” method. We put the name of the column in the square bracket, and it is “Quantity D” here and then utilize the “pd. Series ()” here in which we insert “1” as the constant value. Its length “len” is “Machinery. index”. This “pd. Series” method will add this constant value column in the existing DataFrame. Then, we print “Final Machinery Quantity” by consuming the “print ()”. Lastly, we also print this “Machinery” DataFrame in which we insert the column with a constant value.

When we hit “Shift+Enter”, this result appears on the screen, rendering four columns in the first DataFrame. In the below DataFrame, we have five columns. The last “Quantity D” column is added with the constant value “1” as we have utilized “pd. Series” for adding this column with the constant value.

Example 2:

We have to import the “pandas” as “pd” and create a DataFrame named “result”. We are adding columns which are called “Name”, “Father Name”, and “Obt-Marks”. The names that we put in the “Name” column are “John”, “Thomas”, “Smith”, “George”, and “Allies”. The data we are putting in the “Father Name” column are “Peter”, “Alexander”, “Leo”, “Oscar”, and “Merry”. Now, we add some marks in the “Obt-Marks”, which are “334”, “307”, “225”, “301”, and “299”.

Then, we display a line on the terminal, and after this, we display this DataFrame also. We are inserting two different columns. We add a constant value for each column. We again utilize the “pd. Series ()” in this code, and we put “400” as the constant value for the “Total Marks” column, and we add “Pass” as the constant value for the other column named “Pass/Fail”. We print the “Final result” and display this DataFrame in which we have added two columns with constant values.

We had three columns first, and then we added two new columns. In the next DataFrame, we have five columns. The “Total Marks” column has the constant value “400”, and the “Pass/Fail” column has the constant value “Pass”.

Example 3: As a Static Value

We have utilized the example 2 code in this example and made changes a little bit here. We changed the method for adding a column with the constant value in “pandas”. After printing the first “result” DataFrame, we put “result” and place the name of the column, which is “Pass”, here and put “True”. This will add the “Pass” column with the constant value “True”. Then, we have to render this on the terminal screen, so we utilize the “print ()” again and place “result” in its brackets.

Note the last column in the DataFrame. We have added this column by applying the static value method in “Pandas”. The value of this “Pass” column is constant, and this constant value is “True”.

Example 4: Using the DataFrame.insert () Method

In this code, we are creating the “Grocery” and placing three columns for this DataFrame. These columns are named “Courses”, “Price”, and “Quantity”. We also add the data to each column revealed in the code’s following image. We add “Rice”, “Sugar”, “Milk”, and “Tea” in the first column. In the second column, we put “1200”, “1300”, “2500”, and “3500”. In the last column, we have added “15kgs”, “10kgs”, “5kgs”, and “5kgs”, respectively. We transformed this Grocery into a DataFrame named “Grocery1” and displayed this “Grocery1”, which is the DataFrame here.

We are utilizing the third method, “DataFrame. Insert ()” here. We put the name of the DataFrame and write it as “Grocery1. insert”. Here “2” is used for describing the position where to add this column. We add this column to the “2” number. The name of this column we want to add is “Discount_Percentage” here, and the constant value for this is “10%”. We render it by consuming the “print ()” method here.

The first DataFrame’s three columns are rendered as a result of this. However, there are four columns in the second. As we used the “DataFrame. insert()” method to add this column with the constant value, the column “Discount_Percentage” is created at the “2” position with the constant value “10%”.


The techniques for adding the column with constant value have been explained in great length and straightforwardly in this guide. This guide’s main goal is to assist you in comprehending the idea of “adding a column with the constant value” in pandas. This guide has covered three techniques for adding a column with a constant value. These are straightforward ways to add columns with a constant value in “pandas.” Here, we also extensively detail each concept’s theoretical and practical justifications. We hope they are simple for you to learn this concept from this guide.

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.