How to Remove the Header Row From Pandas Dataframe – Definitive Guide

Dataframe column headers are used to identify columns.

You can remove the header row from the Pandas dataframe using the df.columns = range(df.shape[1]) statement.

This tutorial teaches you the different methods to remove the header row from Pandas dataframe and when it is appropriate to use each method.

Sample Dataframe

Create a sample dataframe with headers.

This dataframe will be used to remove headers using different methods.

import pandas as pd 

users = [('Shivam', 'Pandey', 'India'),
         ('Kumar', 'Ram' , 'India'),
         ('Felix','John' , 'Germany')
        ]

#Create a DataFrame object
df = pd.DataFrame(users, 
                    columns = ['First Name' , 'Last Name', 'Country']
                    ) 

df

DataFrame Will Look Like

First NameLast NameCountry
0ShivamPandeyIndia
1KumarRamIndia
2FelixJohnGermany

Using Range And Shape

The range() function returns a sequence of numbers in a given range.

To remove the column headers and set the column indexes as the header,

  • Pass the range of columns using the df.shape[1] to the range function, and it’ll return the range of numbers.
  • Assign the value to the df.columns attribute.

Code

The following code demonstrates how to use the range function to remove the header and set the column indexes as header.

df.columns = range(df.shape[1])

df

DataFrame Will Look Like

012345
0JohnDoe120 jefferson st.RiversideNJ8075
1JackMcGinnis220 hobo Av.PhilaPA9119
2John “Da Man”Repici120 Jefferson St.RiversideNJ8075
3StephenTyler7452 Terrace “At the Plaza” roadSomeTownSD91234
4NaNBlankmanNaNSomeTownSD298
5Joan “the bone”, AnneJet9th, at Terrace plcDesert CityCO123

Using Dictionary

This section teaches you how to completely remove the header information from the pandas dataframe using a dictionary.

  • Create a dictionary with keys as existing columns and values as empty strings
  • Rename the dataframe using the columns attribute and pass the dictionary, which has the empty string mappings for each column.
  • Use inplace=True to perform the replace operation in the same dataframe instead of creating a new copy.

Code

The following code demonstrates how to use the dictionary to remove header information from the Pandas dataframe.

col_dict = dict.fromkeys(df.columns, '')

df.rename(columns = col_dict, inplace=True)

df

DataFrame Will Look Like

0ShivamPandeyIndia
1KumarRamIndia
2FelixJohnGermany

Remove Header While Reading CSV

To remove header information while reading a CSV file and creating a pandas dataframe, you can use th header=None parameter in the read_csv() method.

  • By default, the read_csv() method considers the first row of the CSV file as the header.
  • While using header=None, the first row will not be considered a header, and the column indexes will be used as a header.

Code

import pandas as pd

df = pd.read_csv('addresses.csv', header=None)

df

DataFrame Will Look Like

012345
0JohnDoe120 jefferson st.RiversideNJ8075
1JackMcGinnis220 hobo Av.PhilaPA9119
2John “Da Man”Repici120 Jefferson St.RiversideNJ8075
3StephenTyler7452 Terrace “At the Plaza” roadSomeTownSD91234
4NaNBlankmanNaNSomeTownSD298
5Joan “the bone”, AnneJet9th, at Terrace plcDesert CityCO123

Additional Resources

Leave a Comment