"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Skip Rows in Pandas CSV Import?

How to Skip Rows in Pandas CSV Import?

Published on 2024-11-18
Browse:727

How to Skip Rows in Pandas CSV Import?

Skipping Rows During CSV Import with Pandas

When using pandas.read_csv() to import CSV data, you may want to skip certain rows. However, the skiprows parameter can be confusing, as it accepts both a list and an integer.

The skiprows parameter allows you to specify rows to skip from the beginning of the file. If you provide a list of row numbers, it will skip those rows. If you provide an integer, it will skip that number of rows.

For example, if you have a CSV file where the second row contains unnecessary data and you want to skip it, you can use any of the following methods:

Skiprow as a List (Recommended)

import pandas as pd
from io import StringIO

s = """1, 2
3, 4
5, 6"""

# Skip the second row using a list
df = pd.read_csv(StringIO(s), skiprows=[1], header=None)

# Output: Row with index 1 skipped
print(df)

Skiprow as an Integer

# Skip the second row using an integer
df = pd.read_csv(StringIO(s), skiprows=1, header=None)

# Output: Row with index 1 skipped
print(df)

Note that using skiprows=1 skips the first row, while skiprows=[1] skips the row with index 1. This is because Python uses 0-based indexing, where the first element in a list has index 0.

Conclusion

By understanding the behavior of the skiprows parameter, you can effectively skip unwanted rows during CSV import using pandas.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3