Erase blank rows while reading csv file

Viewed 7461

I have tried to delete blank rows from my cvs file, however this is not working, it only writes out the first line

please take a look and tell me how i can get all the rows with text and skip the rows that are blank

Here is the code: file I just reads out the first line of the csv file

Thank you in advance!

4 Answers

With the latest pandas (v 1.3.0), there is an argument where you can tell it to skip blank rows. It's enabled by default, but if you want to make it True anyway (e.g. self-documenting code), just set that flag to True. This is from the doc: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

skip_blank_lines: bool, default True
      If True, skip over blank lines rather than interpreting as NaN values.

So, in your code it is:

df = pd.read_csv(path, sep = ';', skip_blank_lines=True)
Related