Home » Pandas: How to Only Read Specific Rows from CSV File

Pandas: How to Only Read Specific Rows from CSV File

by Tutor Aspire

You can use the following basic syntax to only read in specific rows from a CSV file into a pandas DataFrame:

#specify rows to import
specific_rows = [0,2,3]

#import specific rows from CSV into DataFrame
df = pd.read_csv('my_data.csv', skiprows = lambda x: x not in specific_rows)

This particular example will read the rows in index positions 0, 2, and 3 from a CSV file called my_data.csv into a pandas DataFrame.

The following example shows how to use this syntax in practice.

Example: Only Read Specific Rows from CSV File into Pandas

Suppose we have the following CSV file called basketball_data.csv:

If we use the read_csv() function, pandas will automatically import each row from the CSV file into a DataFrame:

import pandas as pd

#import all rows of CSV into DataFrame
df = pd.read_csv('basketball_data.csv')

#view DataFrame
print(df)

  team  points  rebounds
0    A      22        10
1    B      14         9
2    C      29         6
3    D      30         2

However, we can use the following syntax to only import the rows in index positions 0, 2, and 3 from the CSV file into a pandas DataFrame:

import pandas as pd

#specify rows to import
specific_rows = [0,2,3]

#import specific rows from CSV into DataFrame
df = pd.read_csv('basketball_data.csv', skiprows = lambda x: x not in specific_rows)

#view DataFrame
print(df)

  team  points  rebounds
0    B      14         9
1    C      29         6

Notice that only the rows in index positions 0, 2, and 3 from the CSV file are imported into the DataFrame.

This syntax uses the skiprows argument and a lambda function to tell pandas which rows not to skip when importing the CSV file.

In this example, we tell pandas not to skip the rows in index positions 0, 2, and 3 but to skip all other rows when importing the CSV file.

Note: You can find the complete documentation for the pandas read_csv() function here.

Additional Resources

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

Pandas: How to Skip Rows when Reading CSV File
Pandas: How to Append Data to Existing CSV File
Pandas: How to Specify dtypes when Importing CSV File
Pandas: How to Set Column Names when Importing CSV File

You may also like