How to skip rows while reading csv file using Pandas

Python's Pandas library provides the read_csv() function to read CSV files with flexible options for skipping rows. This is useful for data cleaning, removing headers, or filtering specific rows during data import.

Syntax

pandas.read_csv('filename.csv', skiprows=condition)

Parameters:

  • filename.csv Path to the CSV file

  • skiprows Rows to skip. Can be an integer, list, or lambda function

Creating Sample Data

Let's create a sample CSV file for demonstration ?

import pandas as pd

# Create sample data
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'],
    'Age': [25, 30, 35, 28, 32, 45],
    'City': ['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'Berlin']
}

df_sample = pd.DataFrame(data)
df_sample.to_csv('sample.csv', index=False)
print("Sample CSV created:")
print(df_sample)
Sample CSV created:
      Name  Age      City
0    Alice   25  New York
1      Bob   30    London
2  Charlie   35     Paris
3    Diana   28     Tokyo
4      Eve   32    Sydney
5    Frank   45    Berlin

Method 1: Skip Single Row

Skip the first row (index 0) ?

import pandas as pd

# Skip first row
df = pd.read_csv('sample.csv', skiprows=1)
print(df)
    Alice  25  New York
0     Bob  30    London
1 Charlie  35     Paris
2   Diana  28     Tokyo
3     Eve  32    Sydney
4   Frank  45    Berlin

Method 2: Skip Multiple Specific Rows

Skip rows at specific indices using a list ?

import pandas as pd

# Skip rows at index 1 and 3 (Bob and Diana)
df = pd.read_csv('sample.csv', skiprows=[1, 3])
print(df)
      Name  Age      City
0    Alice   25  New York
1  Charlie   35     Paris
2      Eve   32    Sydney
3    Frank   45    Berlin

Method 3: Skip Rows Using Lambda Function

Skip odd-numbered rows using a lambda function ?

import pandas as pd

# Skip odd rows (keep even-indexed rows)
df = pd.read_csv('sample.csv', skiprows=lambda x: x % 2 != 0)
print(df)
      Name  Age      City
0    Alice   25  New York
1  Charlie   35     Paris
2      Eve   32    Sydney

Method 4: Skip First N Rows

Skip the first 3 rows ?

import pandas as pd

# Skip first 3 rows
df = pd.read_csv('sample.csv', skiprows=3)
print(df)
   Diana  28   Tokyo
0    Eve  32  Sydney
1  Frank  45  Berlin

Method 5: Combine skiprows with nrows

Skip specific rows and limit the number of rows to read ?

import pandas as pd

# Skip row 2, read only 3 rows total
df = pd.read_csv('sample.csv', skiprows=[2], nrows=3)
print(df)
     Name  Age      City
0   Alice   25  New York
1     Bob   30    London
2   Diana   28     Tokyo

Comparison

Method Parameter Use Case
Single Row skiprows=1 Skip header or first row
Multiple Rows skiprows=[1,3,5] Skip specific rows
Lambda Function skiprows=lambda x: x%2!=0 Conditional row skipping
First N Rows skiprows=3 Skip headers or metadata

Conclusion

The skiprows parameter in read_csv() provides flexible options for data filtering during import. Use integers for simple skipping, lists for specific rows, or lambda functions for conditional logic based on your data cleaning needs.

Updated on: 2026-03-27T07:45:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements