Articles

Add header to pandas DataFrame

Add header to pandas DataFrame


In this article we will discuss how to add a header to pandas dataframe.

 

Introduction

DataFrame in pandas 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 able to create this 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

 

ExampleCreate 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([['c-001','c-021','c-002','c-004'],

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

                   ["guntur","guntur","guntur","guntur"],

                   [1200,3422,5644,670]

                   ])

#display the dataframe
print(data)

Output:

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

Header refers to columns in the pandas dataframe.

Method-1 : Using columns

By using columns method, we will add a header to the dataframe. It will take the column labels through a list.

Syntax:

data.columns=[columns]

where, data is the input dataframe

columns refers to the column labels

Example:

In this example, we will add columns to the above dataframe - ['col1','col2','col3','col4']

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame([['c-001','c-021','c-002','c-004'],

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

                   ["guntur","guntur","guntur","guntur"],

                   [1200,3422,5644,670]

                   ])

#add header
data.columns=['col1','col2','col3','col4']

#display
print(data)

Output:

                col1    col2      col3               col4
0              c-001   c-021     c-002              c-004
1  vignan university    vvit  RVR - JC  Andhra University
2             guntur  guntur    guntur             guntur
3               1200    3422      5644                670

Method-2 : Using set_axis()

By using set_axis() method, we will add a header to the dataframe. It will take the column labels through a list.

Syntax:

data.set_axis=([columns],axis=1)

where, data is the input dataframe

columns refers to the column labels and axis=1 refers to column axis.

Example:

In this example, we will add columns to the above dataframe - ['col1','col2','col3','col4']

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame([['c-001','c-021','c-002','c-004'],

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

                   ["guntur","guntur","guntur","guntur"],

                   [1200,3422,5644,670]

                   ])

#add header
data=data.set_axis(['col1','col2','col3','col4'],axis=1)

#display
print(data)

Output:

                col1    col2      col3               col4
0              c-001   c-021     c-002              c-004
1  vignan university    vvit  RVR - JC  Andhra University
2             guntur  guntur    guntur             guntur
3               1200    3422      5644                670

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 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
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!