Skip to content Skip to sidebar Skip to footer

Removing Empty Dataframes With Pandas

I have written the following code to use regex to request pages, and look for strings that resemble interest rates. The overall code works; however, it is creating multiple empty d

Solution 1:

How about you just don't print/use the empty ones?

if df.empty:
  continue

Or

if not df.empty:
  print(df)

Solution 2:

if df.dropna(how='all').empty:
    continue

as per https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.Series.empty.html a df with only nans will return False for .empty so if that matters good to use dropna first. You can use 'any' if having any NaN is too much or 'all' if you only want to drop a row/column if its all NaNs (probably what you want)

Post a Comment for "Removing Empty Dataframes With Pandas"