How To Add Header To Pandas Dataframe?

Pandas dataframe is a two-dimensional data structure that is used to store values in row and column format. The rows and columns can have labels that can be used to access them. Row labels are called indexes, and Column labels are known as headers.

You can add header to pandas dataframe using the df.colums = [‘Column_Name1’, ‘column_Name_2’] method.

If you’re in Hurry

You can use the following code snippet to set column headers to the dataframe.

Snippet

df.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width"]

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to add column names to the dataframe.

Sample Dataframe (Dataframe Without Header)

This is the sample dataframe used throughout the tutorial.

You’re loading the iris dataset from the sklearn datasets library and creating pandas dataframe out of it. When creating, it doesn’t have the headers to it.

Snippet

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data)

df.head()

You can print the dataframe using df.head(), and you’ll see the first 5 rows of the dataframe.

Dataframe Will Look Like

0123
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2

Adding Header To Existing Pandas Dataframe Using Columns Attribute

The columns attribute sets the header to the pandas dataframe.

  • You can assign the column names as a list to this attribute
  • The size of the list must be equal to the number of columns in the Pandas dataframe. Otherwise, you’ll get ValueError.

Code

df.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width"]

df.head()

Dataframe Will Look Like

sepal_lengthsepal_widthpetal_lengthpetal_width
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2

This is how you can use the columns attribute to insert headers to the dataframe.

Adding Header To Existing Pandas Dataframe Using Set_Axis() Method

In this section, you’ll learn how the set_axis() method sets the column headers of the dataframe. As per the doc, it is used to set the index of the specified axis.

In this context, you’ll use it to set the index of the column axes.

It accepts three parameters.

  • Columns_names_list – List of column names to be assigned to the dataframe
  • axis=1 – To specify that the label needs to be set for the column axes
  • inplace=True – To specify the changes must be made in the same dataframe rather than creating a new dataframe

Code

The following code demonstrates how to use the set_axis() method to set the header.

df.set_axis(["sepal_length(cm)", "sepal_width(cm)", "petal_length(cm)", "petal_width(cm)"], axis=1, inplace=True)

df.head()

Dataframe Will Look Like

sepal_length(cm)sepal_width(cm)petal_length(cm)petal_width(cm)
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2

This is how you can add a title to the columns in the pandas dataframe.

Add Multilevel Column Header

Pandas dataframe can have multiple column headers for columns or rows.

In this section, you’ll learn how to add a multilevel column header.

The dataframe created in the above sections contains headers already.

Now, you’ll add the second-level column header.

  • Use the set_index() method
  • Use the parameter append=True additionally to add the column names in the next level rather than replacing the existing column names

Code

The following code demonstrates how to add multi-level headers to the pandas dataframe.

df['Flower Type'] = 'Iris'

df = df.set_index('Flower Type', append=True).unstack('Flower Type')

df.head()

Dataframe Will Look Like

sepal_length(cm)sepal_width(cm)petal_length(cm)petal_width(cm)
Flower TypeIrisIrisIrisIris
15.13.51.40.2Iris-setosa
24.93.01.40.2Iris-setosa
34.73.21.30.2Iris-setosa
44.63.11.50.2Iris-setosa
55.03.61.40.2Iris-setosa

This is how you can add a multilevel column header to the existing pandas dataframe.

Conclusion

To summarize, you’ve learned how to add a header to the existing pandas dataframe using the df.column attribute and the df.set_axis() method. You’ve also learned how to set column names while reading the CSV file to create a pandas dataframe.

Also, you’ve set the multilevel column names for the dataframe using the set_index() method.

If you have any questions, comment below.

You May Also Like

Leave a Comment