pandas.read_csv: how to skip empty lines

Let us suppose that we start with a CSV file that has empty rows:

A, B, C
 
1, 2, 3

If you read this file with Pandas library, and look at the content of your dataframe, you have 2 rows including the empty one that has been filled with NAs

>>> import pandas as pd
>>> df = pd.read_csv("test.csv", sep=",")
>>>> print(df)
    A   B   C
0 NaN NaN NaN
1   1   1   1
 
[2 rows x 3 columns]

There is no option to ignore the row in the function read_csv, so you need to do it yourself. Hopefully, there is a dropna method that is handy:

df.dropna(how="all", inplace=True)
Please follow and like us:
This entry was posted in Python and tagged . Bookmark the permalink.

2 Responses to pandas.read_csv: how to skip empty lines

  1. Anonymous says:

    do you try ?

    df = pd.read_csv(“test.csv”, sep=”,”, comment=’ ‘ )

Leave a Reply

Your email address will not be published.