View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Adding New Column To Existing Dataframe In Pandas [2024]

By Rohit Sharma

Updated on Nov 23, 2022 | 7 min read | 36.1k views

Share:

Python, an interpreted, general-purpose, high-level programming language, has recently become a phenomenal computing language due to its vast collection of libraries and easy to implement nature. The popularity of Python took an enormous leap with the implementation of data science and data analytics. There are thousands of libraries that can be integrated with Python to make it work on any vertical efficiently.

Pandas is one such data analytics library designed explicitly for Python to perform data manipulation and data analysis. The Pandas library consists of specific data structures and operations to deal with numerical tables, analyzing data, and work with time series. In this article, you will get to know how to add columns to DataFrame in Pandas that already exists.

Our learners also read: Free Python Course with Certification

What is DataFrame?

Before knowing about how to add a new column to the existing DataFrame, let us first take a glimpse of DataFrames in Pandas. DataFrame is a mutable data structure in the form of a two-dimensional array that can store heterogeneous values with labeled axes (rows and columns). DataFrame is a data structure where the data remains stored in a logical arrangement of tabular (intersecting rows and columns) fashion. The three major components of a DataFrame are rows, columns, and data. Creating a DataFrame in Python is very easy.

import pandas as pd

l = [‘This’, ‘is’, ‘a’, ‘List’, ‘preparing’, ‘for’, ‘DataFrame’]

datfr = pd.DataFrame(l)

print(datfr)

The above program will create a DataFrame of 7 rows and one column.

Also read: Excel online course free!

How to Add Columns to Existing DataFrames?

There are various ways of adding new columns to a DataFrame in Pandas. We have already gathered an idea of how to create a basic DataFrame using the Pandas library. Let us now prepare an already existing library and work on it.

import pandas as pd

# Define a dictionary containing Professionals’ data

datfr = {‘Name’: [‘Karl’, ‘Gaurav’, ‘Ray’, ‘Mimo’],

‘Height’: [6.2, 5.7, 6.1, 5.9],

‘Designation’: [‘Scientist’, ‘Professor’, ‘Data Analyst’, ‘Security Analyst’]}

df = pd.DataFrame(datfr)

print(df)

Output:

Read: Python Pandas Tutorial

Technique 1: insert() Method 

Now, to add new columns to the existing DataFrame, we have to use the insert() method. Before implementing the insert() method, let us know about its working. The DataFrame.insert() allows adding a column at any position the data analyst wants to. It also accommodates several possibilities for injecting the column values. Programmers can specify the index to inject the column of data at that particular position.

import pandas as pd

# Define a dictionary containing Professionals’ data

datfr = {‘Name’: [‘Karl’, ‘Gaurav’, ‘Ray’, ‘Mimo’],

‘Height’: [6.2, 5.7, 6.1, 5.9],

‘Designation’: [‘Scientist’, ‘Professor’, ‘Data Analyst’, ‘Security Analyst’]}

df = pd.DataFrame(datfr)

df.insert(3, “Age”, [40, 33, 27, 26], True)

print(df)
background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months

Placement Assistance

Certification8-8.5 Months

It will add the ‘Age’ column in the third index position as defined in the insert() method as the first parameter.

Technique 2: assign() Method 

Another method to add a column to DataFrame is using the assign() method of the Pandas library. This method uses a different approach to add a new column to the existing DataFrame. Dataframe.assign() will create a new DataFrame along with a column. Then it will append it to the existing DataFrame.\

import pandas as pd

datfr = {‘Name’: [‘Karl’, ‘Gaurav’, ‘Ray’, ‘Mimo’],

‘Height’: [6.2, 5.7, 6.1, 5.9],

‘Designation’: [‘Scientist’, ‘Professor’, ‘Data Analyst’, ‘Security Analyst’]}


dfI = pd.DataFrame(datfr)

dfII = dfI.assign(Location = [‘Noida’, ‘Amsterdam’, ‘Cambridge’, ‘Bangaluru’])

print(dfII)

OUTPUT:

Technique 3: Creating New List as Column 

The last method that programmers can use to add a column to DataFrame is by generating a new list as a separate column of data and appending the column to the existing DataFrame.

import pandas as pd

datfr = {‘Name’: [‘Karl’, ‘Gaurav’, ‘Ray’, ‘Mimo’],

‘Height’: [6.2, 5.7, 6.1, 5.9],

‘Designation’: [‘Scientist’, ‘Professor’, ‘Data Analyst’, ‘Security Analyst’]}

df = pd.DataFrame(datfr)

loc = [‘Noida’, ‘Amsterdam’, ‘Cambridge’, ‘Bangaluru’]

df[‘Location’] = loc

print(df)

OUTPUT:

upGrad’s Exclusive Data Science Webinar for you –

How upGrad helps for your Data Science Career?

Conclusion

Data analysts perform a primary operation for adding an extra set of data in a column-wise form. There are different approaches a data analyst or a programmer can use to add a new column to an existing DataFrame in Pandas. These methods will make programmers handy to add data columns at any point in time while analyzing Pandas data.

If you are curious to learn about DataFrame in Pandas, check out IIIT-B & upGrad’s Executive PG Program in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Frequently Asked Questions (FAQs)

1. Why is Pandas one of the most preferred libraries to create data frames in Python?

2. What are the other Python libraries with whom Pandas library works?

3. Along with insertion, what are the fundamental operations of the Dataframe?

Rohit Sharma

711 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months