Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (17.6k points)

How to check whether a pandas DataFrame is empty? In my case I want to print some message in terminal if the DataFrame is empty.

2 Answers

0 votes
by (41.4k points)

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

if df.empty:

    print('DataFrame is empty!')

0 votes
by (106k points)

To check whether a pandas  DataFrame si empty by using the len function. It's much faster than empty. len(df.index) is even faster.

import pandas as pd 

import numpy as np 

df = pd.DataFrame(np.random.randn(10000, 4), columns=list('ABCD')) 

def empty(df): 

return df.empty 

def lenz(df):

return len(df) == 0 

def lenzi(df):

return len(df.index) == 0

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)

Browse Categories