Home » Pandas: How to Skip Rows when Reading CSV File

Pandas: How to Skip Rows when Reading CSV File

by Tutor Aspire

You can use the following methods to skip rows when reading a CSV file into a pandas DataFrame:

Method 1: Skip One Specific Row

#import DataFrame and skip 2nd row
df = pd.read_csv('my_data.csv', skiprows=[2])

Method 2: Skip Several Specific Rows

#import DataFrame and skip 2nd and 4th row
df = pd.read_csv('my_data.csv', skiprows=[2, 4])

Method 3: Skip First N Rows

#import DataFrame and skip first 2 rows
df = pd.read_csv('my_data.csv', skiprows=2)

The following examples show how to use each method in practice with the following CSV file called basketball_data.csv:

Example 1: Skip One Specific Row

We can use the following code to import the CSV file and skip the second row:

import pandas as pd

#import DataFrame and skip 2nd row
df = pd.read_csv('basketball_data.csv', skiprows=[2])

#view DataFrame
df

        team	points	rebounds
0	A	22	10
1	C	29	6
2	D	30	2

Notice that the second row (with team ‘B’) was skipped when importing the CSV file into the pandas DataFrame.

Note: The first row in the CSV file is considered to be row 0.

Example 2: Skip Several Specific Rows

We can use the following code to import the CSV file and skip the second and fourth rows:

import pandas as pd

#import DataFrame and skip 2nd and 4th rows
df = pd.read_csv('basketball_data.csv', skiprows=[2, 4])

#view DataFrame
df

        team	points	rebounds
0	A	22	10
1	C	29	6

Notice that the second and fourth rows (with team ‘B’ and ‘D’) were skipped when importing the CSV file into the pandas DataFrame.

Example 3: Skip First N Rows

We can use the following code to import the CSV file and skip the first two rows:

import pandas as pd

#import DataFrame and skip first 2 rows
df = pd.read_csv('basketball_data.csv', skiprows=2)

#view DataFrame
df

        B	14	9
0	C	29	6
1	D	30	2

Notice that the first two rows in the CSV file were skipped and the next available row (with team ‘B’) became the header row for the DataFrame.

Additional Resources

The following tutorials explain how to perform other common tasks in Python:

How to Read Excel Files with Pandas
How to Export Pandas DataFrame to Excel
How to Export NumPy Array to CSV File

You may also like