0
0
PandasHow-ToBeginner · 3 min read

How to Skip Rows in read_csv in pandas - Simple Guide

Use the skiprows parameter in pandas.read_csv() to skip rows while loading a CSV file. You can pass an integer to skip that many rows from the start or a list of row indices to skip specific rows.
📐

Syntax

The skiprows parameter in pandas.read_csv() controls which rows to skip when reading a CSV file.

  • skiprows=int: skips the first int rows.
  • skiprows=list: skips rows at the specified indices (0-based).
  • Rows skipped are not included in the resulting DataFrame.
python
pandas.read_csv(filepath_or_buffer, skiprows=None, ...)

# Examples:
# skiprows=3  # skips first 3 rows
# skiprows=[0,2,5]  # skips rows 0, 2, and 5
💻

Example

This example shows how to skip the first 2 rows and also how to skip specific rows by index.

python
import pandas as pd
from io import StringIO

csv_data = '''
Name,Age,City
Skip this line
Skip this too
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago
'''

# Skip first 2 rows
df_skip_first = pd.read_csv(StringIO(csv_data), skiprows=2)

# Skip specific rows: 1 and 2 (the 'Skip this line' and 'Skip this too')
df_skip_list = pd.read_csv(StringIO(csv_data), skiprows=[1,2])

print('Skip first 2 rows:')
print(df_skip_first)
print('\nSkip rows 1 and 2:')
print(df_skip_list)
Output
Skip first 2 rows: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles 2 Charlie 35 Chicago Skip rows 1 and 2: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles 2 Charlie 35 Chicago
⚠️

Common Pitfalls

Common mistakes when using skiprows include:

  • Skipping the header row unintentionally, which causes pandas to treat the next row as header.
  • Using 1-based row numbers instead of 0-based indices in the list.
  • Confusing skiprows with header parameter.

Always check if the header row is skipped and adjust header parameter if needed.

python
import pandas as pd
from io import StringIO

csv_data = '''
Name,Age,City
Skip this line
Alice,30,New York
Bob,25,Los Angeles
'''

# Wrong: skips header row unintentionally
try:
    df_wrong = pd.read_csv(StringIO(csv_data), skiprows=1)
    print('Wrong skiprows=1 result:')
    print(df_wrong)
except Exception as e:
    print('Error:', e)

# Right: skip row 1 but keep header at row 0
# Use header=0 explicitly

df_right = pd.read_csv(StringIO(csv_data), skiprows=[1], header=0)
print('\nRight skiprows=[1] with header=0 result:')
print(df_right)
Output
Wrong skiprows=1 result: Skip this line Alice 30 New York 0 Bob 25 Los Angeles Right skiprows=[1] with header=0 result: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles
📊

Quick Reference

ParameterDescriptionExample
skiprows=intSkip first n rows from the startskiprows=3
skiprows=listSkip specific rows by 0-based indexskiprows=[0,2,5]
header=intRow number to use as column namesheader=0
skipfooter=intSkip rows at the end of fileskipfooter=2 (requires engine='python')

Key Takeaways

Use skiprows=int to skip the first n rows when reading a CSV with pandas.
Use skiprows=list to skip specific rows by their zero-based indices.
Be careful not to skip the header row unless you adjust the header parameter accordingly.
skiprows only affects which rows are ignored; it does not change the header row by default.
For skipping rows at the end, use skipfooter with engine='python'.