Pandas: How can I only read specific rows from a CSV file?

One way to only read specific rows from a CSV file is to use the pandas read_csv() function and use the skiprows parameter to specify the rows you want to skip. This parameter takes a list of row numbers to skip and can be used to select a specific subset of rows from the CSV file. By using the skiprows parameter, you can quickly and easily read only the rows you are interested in from a CSV file.


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 .

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

x