Home  >  Article  >  Backend Development  >  How to Skip Rows in Pandas CSV Import?

How to Skip Rows in Pandas CSV Import?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 22:07:02409browse

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)

<code class="python">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)</code>

Skiprow as an Integer

<code class="python"># 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)</code>

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.

The above is the detailed content of How to Skip Rows in Pandas CSV Import?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn