Check if a Pandas DataFrame is Empty

The attribute df.empty in Pandas allows users to easily check if a DataFrame is empty or not. It returns a boolean value, "True" if the DataFrame is entirely empty with no items, indicating that any of its axes (rows or columns) have a length of 0. This attribute is useful for conditional checks to handle empty DataFrames in data processing and analysis tasks.

import pandas as pd import numpy as np df = pd.DataFrame({'Name' : []}) if df.empty: print('DataFrame is empty!') else: print('Not empty!')
DataFrame is empty!

If a DataFrame contains only NaNs (missing values) and no actual data points, it is not considered empty by Pandas. The df.empty attribute checks if the DataFrame has any items at all, and even if all the items are NaNs, the DataFrame is not considered empty because it still has a defined structure with rows and columns. To specifically check if a DataFrame contains only NaNs, you can use the isna() method along with any() or all() to perform element-wise checks for NaN values across the DataFrame.

import pandas as pd import numpy as np df = pd.DataFrame({'Name' : [np.nan]}) if df.empty: print('DataFrame is empty!') else: print('Not empty!')
Not empty!

Using len()

Using the len() function is a faster way to check if a DataFrame is empty compared to using the df.empty attribute. The len() function returns the number of rows in the DataFrame, and if the DataFrame is empty, it will return 0. This method is faster because it only needs to check the number of rows, whereas df.empty checks for any items in the DataFrame, which involves more internal operations.

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill'] df['Promoted'] = [True, False,True] df['Marks'] = [82, 38, 63] df.drop(df.index, inplace=True) if len(df.index) == 0: print('DataFrame is empty!') else: print('Not empty!')
DataFrame is empty!

Conclusion

To check if a Pandas DataFrame is empty, you can use either the df.empty attribute or the len() function. The df.empty attribute returns True if the DataFrame has no items (0 rows or columns), while the len() function returns the number of rows and will be 0 if the DataFrame is empty. The len() function is generally faster and more efficient for this purpose, especially when dealing with large DataFrames.