Pandas Add Column

Among the most popular tools for data clean-up and investigation in data science and machine learning is called Pandas. Pandas are the ideal approach to handle this chaotic real-world data in this situation. The “pandas add columns” means we add columns in pandas. We can easily add columns in pandas. We have different methods to add columns in Pandas. In this article, we will utilize those methods and also explain them in detail. We will add columns in pandas with the help of those methods.

Methods for Adding Columns in Pandas

The methods which we will discuss in this article for adding new columns in Pandas are given below:

Now, we are moving to our examples where we will utilize these methods separately in each example. We will explore all four methods in depth by utilizing them in our codes here.

Example 01: Declare the List as the Column Method

We do these codes in “Spyder” software. We install this software and then start coding on this “Spyder” software. We import the “pandas” as “pd”. The “import pandas” line of the code instructs Python to import the computational library into the current program. The “as pd” section of the code line directs Python to give pandas the “pd” acronym. Therefore, you may utilize the pandas’ methods by just typing “pd”.

After this, we are defining the dictionary with the name “info” containing some data. We add the “Name”, “Age”, and “Qualification” of some members in this dictionary. The names we are adding in this name field are “David”, “John”, “Allies”, “William”, “Emma”, and “Noah”. The ages we are adding in the age field are “22”, “27”, “24”, “25”, “23”, and “22”. We also add the qualification data in the qualification field and these are “BS”, “MCS”, “BBA”, “MA”, “BS-IT”, and “MBA” respectively.

After adding these data to a dictionary, we are converting that dictionary into the “DataFrame”. We use a variable “df1” and initialize it with a “pd. DataFrame” and give the name of the above dictionary to this “DataFrame” for converting it into the “DataFrame”. We print this DataFrame “df1” by using “print ()” here. After this, we want to display a line so for this, we again put the “print ()” statement. We are adding a column to the DataFrame which we have created yet. We are applying the first method here for adding a column. We declare a column here. We declare it with the name “Location” and also initialize it with data. We add the location of the members here in this new column and we will add this column to the current DataFrame. We put “df1[‘Location’]” and assign “Location” to this.

Now, we are placing the “print ()” statement below and putting “df1”. So, it will render the DataFrame and also add this new column to this DataFrame.

The output is also provided here. We get this by simply hitting “Shift+ Enter” and this outcome is rendered on the terminal. You may press the “run” button of this “Spyder” software for this output. In the first Dataframe, there are three columns. In the second DataFrame, there are four columns. We add this new column to this DataFrame by utilizing the first method.

Example 02: DataFrame.insert() Method

Start this code by importing pandas packages and then define a dictionary below this. We add two fields or columns to this dictionary. We named this dictionary “info1”. We add “name” and “age” to this “info1” dictionary. Then, we covert this dictionary to the “DataFrame” in Pandas. We name this DataFrame as “df2” and print that “df2” DataFrame. We also print a line below this DataFrame. After this, we are adding two different columns in this DataFrame with the help of the “DataFrame. Insert()” method. This method will help us to add columns in the DataFrame.

We mention the name of the DataFrame and put “insert”. The first parameter of this insert method defines the position of the column where to add this new column. So, we put “2” and we give the name of the column which we want to add and this name is “address”. After this, we give the addresses inside the square bracket and place “True” as the last parameter. Now, we have to add another column with the help of the same “DataFrame. Insert()” method. So, we again put the name of the DataFrame and in the insert method we give the position as “3” and the name of the other column which we want to add here is “Qualification”. We insert this “Qualification” column in the same way as we have added the “Address”. After adding both columns, we print this new DataFrame with the usage of the “print ()” method.

This outcome renders the first DataFrame which contains two columns. In the second DataFrame, there are four columns, as we have added the last two columns by utilizing the “DataFrame. Insert()” method.

Example 03: DataFrame.assign() Method

Import the “pandas” as “pd” in the code’s first line and create a dictionary named “info2” in which we have added two columns. We convert this “info2” into the DataFrame and we assign the “df3” name to this DataFrame. We print this “DataFrame” before adding a new column. After this, we print a statement and utilize the third method which is the “DataFrame. Assign()” method. We named this new DataFrame as “df”. We add the “Height” by utilizing the “df2. Assign()” method. We put “Height” and also add the heights in the square brackets. And also print the DataFrame in which this “Height” column is also added.

In the first DataFrame, you can easily notice that there is no column of “Height” as we have printed this Datarame before adding the “Height” column. In the second DataFrame, we have the “Height” column. We added this column with the help of the “DataFrame. Assign ()” method.

Example 04: By Utilizing a Dictionary

First, we import the pandas and then define the dictionary named “info2”. After this, we define another new dictionary named “Qualification”. We are adding the values of the column which we have created in the above dictionary and also adding the respective values with these key values. As we write ‘BS’: ‘David’, it will add this “BS” qualification with the “David”. We add all new values here in this way and then convert them to “DataFrame”. We are converting this dictionary to the new DataFrame by utilizing the “pd. DataFrame (info2)”. After this, we give the “Qualification” as the name of the new column and print it on the terminal screen.

Here, the Qualification column is also added to the DataFrame. We add this column by utilizing a new dictionary and adding it to the existing dictionary.


This article was created to explain the idea behind the “pandas add column” method. We went into great depth to describe this idea so you could “add the column” with ease. We have explained four different methods for adding columns in Pandas. We have utilized all four methods here and explained all of them in detail. After carefully reading this article, I hope you will understand this idea.

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.