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)
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Skiprow as an Integer
1 2 3 4 5 |
|
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!