Articles

Add column to existing Pandas DataFrame | Add empty, single, multiple columns pandas

Add column to existing Pandas DataFrame | Add empty, single, multiple columns pandas


In this Pandas tutorial, we will discuss about:

  • add column to existing pandas dataframe by declaring new list
  • add column to existing pandas dataframe using assign() function
  • add multiple columns to existing dataframe pandas,
  • add empty columns to dataframe pandas

Lets first understand about DataFrame and create a DataFrame before starting with how to add multiple columns to existing dataframe in pandas?

 

What is DataFrame?

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can create DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe.

 

The data can be a dictionary that stores list of values with specified key


Create Pandas DataFrame

In this example, we will create a dataframe with 4 rows and 4 columns with college data

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })


#display the dataframe
print(data)

Output: Below DataFrame is created by above code snippet

  college_id       college_name college_address  Total Staff
0      c-001  vignan university          guntur         1200
1      c-021               vvit          guntur         3422
2      c-002           RVR - JC          guntur         5644
3      c-004  Andhra University          guntur          670

Lets see the different methods to add column to existing pandas dataframe.


Method 1  - Add column to existing pandas dataframe declaring new list as a column

In this case, we are creating values for the newly created column in a list and assign the list to new column by specifying column name to the dataframe.

Syntax:

dataframe.[‘new_column’]=list

where,

1.      dataframe is the input dataframe

2.      new_column is the column name

3.      list is the input values to this new column

 

Example:

In this example we will learn to add column to existing pandas dataframe. Here, we are going to add Grade column to the above dataframe.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

# add Grade column
data['Grade']=["A","A","A+","A*"]

#display  dataframe
print(data)

Output: Below we can see that above code snippet helps add column to existing pandas dataframe named Grade.

  college_id       college_name college_address  Total Staff   Grade
0      c-001  vignan university          guntur         1200     A
1      c-021               vvit          guntur         3422     A
2      c-002           RVR - JC          guntur         5644     A+
3      c-004  Andhra University          guntur          670     A*

In the above example, we also get to see how to add column names to existing dataframe pandas.


Method 2 – Add column to existing pandas dataframe using assign() function

In this scenario, we are assigning a column with assign() function that will help add column to existing pandas dataframe.

Syntax:

dataframe.assign(new_column=list)

where,

1.      dataframe is the input dataframe

2.      new_column is the column name

3.      list is the input values to this new column

 

Example:

In this example, we will learn to add column to existing pandas dataframe using assign function. Here, we are going to add Grade column to the above dataframe.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

# add Grade column

data=data.assign(Grade=["A","A","A+","A*"])

#display  dataframe
print(data)

OutputBelow we can see that above snippet helps add column to existing pandas dataframe named Grade.

  college_id       college_name college_address  Total Staff   Grade
0      c-001  vignan university          guntur         1200     A
1      c-021               vvit          guntur         3422     A
2      c-002           RVR - JC          guntur         5644     A+
3      c-004  Andhra University          guntur          670     A*

Thus we have learned how to add column names to existing dataframe pandas along with value.


Add multiple columns to existing dataframe pandas

We can also add multiple columns to existing dataframe pandas at a time.

 

Example: In the below we will add two columns to dataframe pandas named Grade and Students.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

# add Grade and students column

data=data.assign(Grade=["A","A","A+","A*"],Students=[2343,4322,567,655])

#display  dataframe
print(data)

Output: Below we can see, above code add multiple columns to existing dataframe pandas - Grade & Student.

Similarly, we can add multiple columns to existing dataframe pandas with any column names.


Add empty columns to dataframe pandas

We can also add a column with no / empty values.

Just place the value as blank- ""

 

Lets see how to add empty columns to dataframe pandas?

Syntax:

data['empty_column_name']=""

Example: In the below we will add empty columns to dataframe pandas named 'empty column'

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   })

# add empty column
data['empty column']=" "

#display  dataframe
print(data)

Output: Result for add empty columns to dataframe pandas is given below

  college_id       college_name college_address  Total Staff empty column
0      c-001  vignan university          guntur         1200             
1      c-021               vvit          guntur         3422             
2      c-002           RVR - JC          guntur         5644             
3      c-004  Andhra University          guntur          670     

Using this approach, we can add empty columns to dataframe pandas.


Conclusion

In this article, we discussed how to add multiple columns to existing dataframe in pandas? The two ways to add a new column is using assign() function and declaring a list. Similarly, we can also add empty columns to dataframe pandas.

 


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 23,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!