pandasperformance

Check that (pandas) dataframe has rows without counting them all


I'd like to know if a DataFrame I'm modifying is empty yet. The obvious way would be to compute its length. But that requires counting every row, which is extremely inefficient. Is there a more elegant way?


Solution

  • You can use the attribute df.empty to check whether it's empty or not:

    if df.empty:
        print('DataFrame is empty!')
    else:
        print('DataFrame is not empty.')