3

Add Column to Pandas DataFrame with constant value

 1 year ago
source link: https://thispointer.com/add-column-to-pandas-dataframe-with-constant-value/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Add Column to Pandas DataFrame with constant value

In this article, we will learn different ways to add a column in a DataFrame with a constant value.

Table Of Contents

Suppose we have a DataFrame,

Rollno Name
r1 1 Reema
r2 2 Rekha
r3 3 Jaya
    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
    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.

Advertisements

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.

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)
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
    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)
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
    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)
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
    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)
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
    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)
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
    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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK