Pandas: How to skip N first/last rows of a CSV file

Updated: February 21, 2024 By: Guest Contributor Post a comment

Introduction

Pandas, an essential library in the Python Data Science stack, provides extensive capabilities to manipulate and analyze data efficiently. In this tutorial, we’ll dive into how to skip N number of rows from the beginning or end of a CSV file while reading it into a DataFrame. This functionality is particularly useful when dealing with large datasets or files with unnecessary header or footer information.

Let’s start with the basics of reading a CSV file in Pandas and progressively cover how to skip rows upon import, using various techniques and parameters.

Basic CSV File Reading

import pandas as pd

df = pd.read_csv('your_file.csv')
print(df.head())

This snippet demonstrates how to load an entire CSV file. Now, we move towards skipping rows dynamically.

Skipping N Top Rows

import pandas as pd

df = pd.read_csv('your_file.csv', skiprows=N)
print(df.head())

In the code above, replace N with the number of rows you want to skip from the top. Note that counting starts from 0, meaning if you set N=1, the first row (often the header) will be skipped.

Skipping Rows Using a Lambda Function

import pandas as pd

df = pd.read_csv('your_file.csv', skiprows=lambda x: x in [0, 2])
print(df.head())

This approach provides more flexibility. Here, we’re skipping the first and third rows (index 0 and 2, respectively) by providing a lambda function to skiprows.

Skipping Footer Rows

import pandas as pd

df = pd.read_csv('your_file.csv', skipfooter=N, engine='python')
print(df.head())

To skip N rows from the end, use the skipfooter parameter. It requires the engine='python' parameter since the default C engine does not support skipfooter. Remember, this operation may slow down the reading process.

Combining Headers and Footers Skipping

import pandas as pd

df = pd.read_csv('your_file.csv', skiprows=N, skipfooter=M, engine='python')
print(df.head())

By combining skiprows and skipfooter, you can skip rows from both the beginning and end of the file. Replace N and M with the respective numbers of rows to skip.

Skipping Rows with Conditionals

import pandas as pd

data_loader = lambda x: x.startswith('#') or (x.strip() == '')
df = pd.read_csv('your_file.csv', comment='#', na_filter=False, skip_blank_lines=True)
print(df.head())

Advanced filtering allows you to skip lines starting with a specific character (like a comment) or blank lines. Use the comment and skip_blank_lines parameters to achieve this.

Use Case: Processing Large Files

import pandas as pd

chunk_size = 50000
total_chunks = 0
for chunk in pd.read_csv('large_file.csv', chunksize=chunk_size, skiprows=N, skipfooter=M, engine='python'):
    total_chunks += 1
    # Process each chunk

print(f'Total chunks processed: {total_chunks}')

When dealing with very large files, you might want to process the data in chunks while still skipping rows. This code shows how to combine chunk processing with row skipping functionality.

Conclusion

Throughout this tutorial, we’ve explored different methods to effectively skip rows at the beginning or end of a CSV file when loading it into a Pandas DataFrame. Understanding these techniques ensures you have the flexibility to handle various data preprocessing tasks efficiently. Keep experimenting with these approaches to find the best fit for your specific data challenges.