How to Drop Header Row of Pandas DataFrame?

When working with data in Pandas, I often encounter situations where the CSV or Excel file I import already has a header row that I don’t want to include in my DataFrame. Other times, I need to remove the column names entirely for specific data processing requirements.

In this article, I’ll show you several easy methods to drop the header row in Pandas, based on my decade-long experience working with Python data analysis.

Let’s start..!

Drop Header Row of Pandas DataFrame

Let me show you how to drop the Header Row of a Pandas DataFrame in Python.

Read Pandas Replace Multiple Values in Column Based on Condition in Python

1 – Use the skiprows Parameter When Reading Files

One of the simplest ways to drop the header row is to skip it during import using the skiprows parameter.

Let’s say we have a CSV file containing sales data from different states in the USA:

import pandas as pd

# Skip the first row (header) when reading the file
df = pd.read_csv('us_sales_data.csv', skiprows=1)

# Display the DataFrame
print(df.head())

Output:

  California  125000  42500
0      Texas   93000  31200

I executed the above example code and added the screenshot below.

pandas remove header

This method is perfect when you want to avoid the header altogether and is especially useful when dealing with files that have multiple header rows or metadata at the top.

You can also skip multiple rows by passing a list:

# Skip first 2 rows (perhaps metadata and header)
df = pd.read_csv('us_sales_data.csv', skiprows=[0, 1])

Check out Count Duplicates in Pandas dataframe in Python

2 – Use header=None and Renaming Columns Later

Sometimes, you want to replace the existing headers with your custom column names:

# Import without considering any row as header
df = pd.read_csv('us_sales_data.csv', header=None)

# Assign your own column names
df.columns = ['State', 'Product', 'Sales', 'Date', 'Customer_Type']

print(df.head())

Output:

        State     Product  Sales        Date    Customer_Type
0       State     Product  Sales        Date  Customer_Type
1  California      Laptop   1200  2024-01-15     Individual
2       Texas  Smartphone    850  2024-01-17       Business
3    New York      Tablet    600  2024-01-20     Individual
4     Florida     Monitor    300  2024-01-22      Business

I executed the above example code and added the screenshot below.

pandas remove headers

This approach gives you complete control over your column names and is particularly helpful when the original headers are unclear or need standardization.

Read Pandas Iterrows Update Value in Python

3 – Drop Header Row After Import

If you’ve already loaded your data and need to remove the first row afterward, you can use the drop() method in Python:

# Import data normally
df = pd.read_csv('us_sales_data.csv')

# Drop the first row (index 0)
df = df.drop(0)

# Reset the index
df = df.reset_index(drop=True)

print(df.head())

Output:

      State     Product  Sales        Date Customer_Type
0     Texas  Smartphone    850  2024-01-17      Business
1  New York      Tablet    600  2024-01-20    Individual
2   Florida     Monitor    300  2024-01-22     Business

I executed the above example code and added the screenshot below.

pandas drop header

This method is valuable when you discover, after import, that the first row contains header information that was improperly read as data.

Read Pandas Iterrows in Python

4 – Use iloc to exclude the First Row

Another elegant way to drop the header row from an existing DataFrame is to use the iloc indexer in Python:

# Original DataFrame
df = pd.read_csv('us_sales_data.csv')

# Select all rows except the first one
df = df.iloc[1:, :]

# Reset index
df = df.reset_index(drop=True)

print(df.head())

The iloc method provides a clean, slicing-based syntax that many Python developers find intuitive and readable.

Check out Pandas Replace Multiple Values in Python

5 – Handle Excel Files with Multiple Header Rows

When working with Excel files that have multiple header rows or metadata, we can use a combination of parameters:

# Skip the first 3 rows and use the 4th row as header
df = pd.read_excel('quarterly_reports.xlsx', 
                   skiprows=3,
                   header=0)  # header=0 means the first row AFTER skipping

print(df.head())

This approach is particularly useful for complex Excel reports with title sections, metadata rows, and formatting before the actual data begins.

Read Pandas Find Index of Value in Python

Real-World Example: Processing US Census Data

Let’s put these methods into practice with a more comprehensive example. Imagine we’re working with US Census data that has multiple header rows and needs cleaning:

import pandas as pd

# Method 1: Skip multiple rows including headers
census_data = pd.read_csv('us_census_data.csv', skiprows=[0, 1, 2])

# Add our own column names
census_data.columns = ['State', 'County', 'Population', 'Median_Age', 
                      'Median_Income', 'Education_Level']

# Let's process some data
# Find states with highest median income
high_income_states = census_data.groupby('State')['Median_Income'].mean().sort_values(ascending=False).head(5)
print("Top 5 states by median income:")
print(high_income_states)

# Find counties with youngest population
young_counties = census_data.sort_values('Median_Age').head(10)
print("\nTop 10 youngest counties:")
print(young_counties[['County', 'State', 'Median_Age']])

In this example, we’re not just dropping header rows but also performing meaningful analysis on the cleaned data – something I do regularly in my data science projects.

Check out Pandas Count Rows with Condition in Python

Additional Tips for Working with Headers in Pandas

Here are some extra tips I’ve gathered from my years of experience:

  1. Check your data first: Always inspect the first few rows before deciding how to handle headers.
   # Peek at the file before importing
   with open('data.csv', 'r') as f:
       for i in range(5):
           print(f.readline())
  1. Handle files with no headers: Sometimes your file has no header at all.
   df = pd.read_csv('raw_data.csv', header=None)
  1. Custom header handling with parsing functions: For complex files, you might need more control.
   # Define a custom parser
   def custom_parser(x):
       # Skip first 2 lines, then read
       lines = x.split('\n')[2:]
       return '\n'.join(lines)

   df = pd.read_csv('complex_file.csv', converters={'column_name': custom_parser})

When working with data from various sources, these techniques have saved me countless hours of manual data cleaning and preprocessing.

I hope these methods help you efficiently handle header rows in your Pandas DataFrames. The right approach often depends on when you discover the header issue and what you need to do with the data afterward. I have explained real-world examples and also additional topics for working with headers in Pandas for better understanding.

Other Pandas-related tutorials.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.