In this article, we will learn different ways to add a column in a DataFrame with a constant value.
Table Of Contents
- Add a DataFrame Column with constant value using DataFrame.insert()
- Example of DataFrame.insert() function
- Add a DataFrame Column with constant value using ‘+’ operator
- Add a DataFrame Column with constant values using DataFrame.apply() and lambda
- Add a DataFrame Column with constant values using DataFrame.assign()
- Add a DataFrame Column with constant values using Pandas Series
Suppose we have a DataFrame,
Rollno Name r1 1 Reema r2 2 Rekha r3 3 Jaya
Now we want to add a new column ‘Semester’ in this DataFrame. But all the values in this new column should be same. Like,
Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
There are different ways to add a column to DataFrame with constant value in Pandas. Let’s discuss them one by one.
Add a DataFrame Column with constant value using DataFrame.insert()
The DataFrame.insert() method can be used to add a new column to the DataFrame at specified position. For that we need to specify the index position of column in the existing DataFrame. The DataFrame index starts from zero. In the other arguments we can pass the column name and values.
Example of DataFrame.insert() function
A pandas script to add constant value 2 to each ‘semester’ to the existing DataFrame.
Frequently Asked:
import pandas as pd student = { 'Rollno':[1,2,3], 'Name' :["Reema","Rekha","Jaya"] } index_labels=['r1','r2','r3'] # Creating a DataFrame df = pd.DataFrame(student,index=index_labels) print(df) default_value = 2 # Adding a new column with same values df.insert(2,'Semester',[default_value] * 3) print(df)
Output
Rollno Name r1 1 Reema r2 2 Rekha r3 3 Jaya Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
In the above script, the DataFrame.insert() function is used to insert new column semester as the 3rd column of DataFrame with same value 2.
Add a DataFrame Column with constant value using ‘+’ operator
We can use the ‘+’ operator to add a constant number to each element of a DataFrame column. We can assign these new Using this approach you can also append a constant string to each element of string column.
Example of adding a constant value to each entry of column using ‘+’ operator
A pandas script to add constant value 2 to each element of column ‘semester’.
import pandas as pd students = { 'Rollno': [1, 2, 3], 'Name' : ["Reema", "Rekha", "Jaya"], 'Semester':[0,0,0] } index_labels=['r1','r2','r3'] # Create a DataFrame df = pd.DataFrame(students, index=index_labels) print(df) # Add a constant value to each element of column df['marks'] = df['Semester'] + 2 print(df)
Output
Rollno Name Semester r1 1 Reema 0 r2 2 Rekha 0 r3 3 Jaya 0 Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
In the above script, ‘+’ operator is applied with each value of semester column and added it as a new column.
Add a DataFrame Column with constant values using DataFrame.apply() and lambda
We can use dataFrame.apply() with a lambda function to add a new column with a constant values.
Example of DataFrame.apply() and lambda function to add new column with constant value
A pandas script to add constant value 2 to each element of column ‘semester’ of an existing dataFrame abd
import pandas as pd student = { 'Rollno':[1,2,3], 'Name' :["Reema","Rekha","Jaya"] } index_labels=['r1','r2','r3'] # Create DataFrame df = pd.DataFrame(student, index = index_labels) print(df) # Add a new column with contant value df['Semester'] = df.apply(lambda x: 2, axis = 1) print(df)
Output
Rollno Name r1 1 Reema r2 2 Rekha r3 3 Jaya Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
In the above script, we have first created DataFrame with two columns Rollno and name. To add a new column Semester DataFrame.apply() function with lambda x:2 is applied ,
2 is the constant value which will be the value for all records of new column Semester
Add a DataFrame Column with constant values using DataFrame.assign()
The DataFrame.assign() function is used to add a new column to the DataFrame with constant values. This function will create returns a DataFrame after adding new column.
Let’s see an example, where we will add a new column ‘Semester’ with a constant value 2.
import pandas as pd student = { 'Rollno':[1,2,3], 'Name' :["Reema","Rekha","Jaya"] } index_labels=['r1','r2','r3'] # Create DataFrame df = pd.DataFrame(student, index=index_labels) print(df) # Add a new column 'Semester' in DataFrame # with contant value 2 in each row df = df.assign(Semester=2) print(df)
Output
Rollno Name r1 1 Reema r2 2 Rekha r3 3 Jaya Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
In the above script, first we have created a DataFrame with two columns Rollno and Name. The we added a new column Semester with contant value 2 using to the existing DataFrame, using DataFrame.assign() function.
Add a DataFrame Column with constant values using Pandas Series
A Pandas Series object can also be added as a new column with constant value in the existing DataFrame. We can create a Pandas series object with similar constant values and then assign it to a new column in DataFrame. It will add the Series object as a new column in DataFrame.
Let’s see an example, where we will add a new column ‘Semester’ with same values.
import pandas as pd students = { 'Rollno':[1,2,3], 'Name' :["Reema","Rekha","Jaya"]} index_labels=['r1','r2','r3'] # Create a DataFrame df = pd.DataFrame(students, index=index_labels) print(df) # Add a new column with contant value 2 df['Semester'] = pd.Series([2 for x in range(len(df.index))]).values print(df)
Output
Rollno Name r1 1 Reema r2 2 Rekha r3 3 Jaya Rollno Name Semester r1 1 Reema 2 r2 2 Rekha 2 r3 3 Jaya 2
Summary
In the article, we learned to add a new column to the DataFrame with constant value. Happy Learning.