Add Header To Pandas Dataframe

This article will discuss how to add a Header to an existing Pandas DataFrame.

A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. In a Dataframe, the header refers to the column names in the dataframe.

Table of Contents

Add Header to Existing Dataframe using columns attribute

Here , we are going to assign the new column names to an existing Dataframe using columns attribute of the dataframe. Syntax is as follows:

dataframe.columns=[column_names]

where,

Advertisements
  • dataframe is the input dataframe
  • columns is the method to assign the column names to the dataframe
  • column_names is the columns to be assigned separated by comma.

Let’s create the dataframe with 4 columns and 4 rows

#import pandas module
import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame([[7058,7069,7060,7061],
                   ['sravan','bobby','ojaswi','deepu'],
                   [21,23,22,21],
                   ['linux','html/css','node-js','php-mysql']])

#display
print(data)

Output:

        0         1        2          3
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

Here By default column names start with 0.

Example: Add Header to the dataframe

#assign the columns
data.columns=["id", "name", "age", "subjects"]

#display
print(data)

Output:

       id      name      age   subjects
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

Here , we assigned the Header – “id”, “name”, “age”, “subjects” to the above dataframe.

Replace existing Header of a Pandas Dataframe

Here , we are going to create the dataframe with column names and we will add new headers to the existing dataframe by replacing the existing column names. So we have to use columns attribute

Syntax:

dataframe.columns=[column_names]

where,

  • dataframe is the input dataframe
  • columns is the method to assign the column names to the dataframe
  • column_names is the columns to be replaced i with the existing columns in the dataframe separated by comma.

Let’s create the dataframe with 4 columns and 4 rows

#import pandas module
import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame([[7058,7069,7060,7061],
                   ['sravan','bobby','ojaswi','deepu'],
                   [21,23,22,21],
                   ['linux','html/css','node-js','php-mysql']],
                   columns=["id", "name", "age", "subjects"])

#display
print(data)

Output:

       id      name      age   subjects
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

Here we will create the dataframe with columns “id”, “name”, “age”, “subjects”

Example: Replace Header in the existing dataframe

#assign the columns
data.columns=["stu_id", "stu_name", "stu_age", "stu_subjects"]

#display
print(data)

Output:

   stu_id  stu_name  stu_age stu_subjects
0    7058      7069     7060         7061
1  sravan     bobby   ojaswi        deepu
2      21        23       22           21
3   linux  html/css  node-js    php-mysql

Here , we assigned the Header – “stu_id”, “stu_name”, “stu_age”, “stu_subjects” to the above dataframe.

Add Header to Pandas Dataframe using set_axis() method

Here , we are going to assign the columns to the existing dataframe. So we have to use set_axis() method. This is used to assign the column names based on axis.
axis=1 specifies columns. So we have to use axis=1

Syntax:

dataframe.set_axis([column_names],axis=1,inplace=True)

where,

  • dataframe is the input dataframe
  • column_names is the columns to be assigned separated by comma.
  • axis=1 specifies column
  • inplace=True is used to get the dataframe with new columns.

Let’s create the dataframe with 4 columns and 4 rows

#import pandas module
import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame([[7058,7069,7060,7061],
                   ['sravan','bobby','ojaswi','deepu'],
                   [21,23,22,21],
                   ['linux','html/css','node-js','php-mysql']])

#display
print(data)

Output:

        0         1        2          3
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

Here By default column names start with 0.

Example: Add Header to the dataframe

#set the column names -
# ["id", "name", "age", "subjects"]
data.set_axis(["id", "name", "age", "subjects"],
              axis=1,
              inplace=True)

#dsiplay dataframe
print(data)

Output:
Here , we assigned the Header – “id”, “name”, “age”, “subjects” to the above dataframe.

       id      name      age   subjects
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

Add Multilevel Column Headers to Pandas Dataframe

We can add column header to the dataframe with existing columns. Adding multilevel means we are adding one more column with value in the dataframe by using set_axis() method .

Syntax:

dataframe['new_header'] = 'value'

dataframe = dataframe.set_index('new_header', append=True).unstack('new_header')

where,

  • dataframe is the input dataframe
  • new_header is the new column
  • value is the column wise value
  • append parameter is used to add this header to the existing column
  • unstack() is used to place the new_header under the actual column.

Example:

Here we are going to create dataframe with columns

#import pandas module
import pandas as pd

# Create the dataframe with 4 columns
data=pd.DataFrame([[7058,7069,7060,7061],
                   ['sravan','bobby','ojaswi','deepu'],
                   [21,23,22,21],
                   ['linux','html/css','node-js','php-mysql']],
                   columns=["id", "name", "age", "subjects"])

#display
print(data)

Output:

       id      name      age   subjects
0    7058      7069     7060       7061
1  sravan     bobby   ojaswi      deepu
2      21        23       22         21
3   linux  html/css  node-js  php-mysql

We are going to add new header named ‘Student details’ with value as ‘college name’ to the existing columns in the dataframe.

#add column named Student details
#and assign the value 'college name'
data['Student details'] = 'college name'

#set the column by appending to the dataframe
#unstack is used to add the column to next level in the header
#of the dataframe
data = data.set_index('Student details', append=True).unstack('Student details')

#display
print(data)

Output:

                          id         name          age     subjects
Student details college name college name college name college name
0                       7058         7069         7060         7061
1                     sravan        bobby       ojaswi        deepu
2                         21           23           22           21
3                      linux     html/css      node-js    php-mysql

Summary

This article discussed four methods for adding the header to the dataframe using columns attribute with examples.

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top