Built with Tutify.io

    AIStudyLabs

    Pandas Check If DataFrame is Empty | Examples

    Pandas Check If DataFrame is Empty | Examples

    By using pandas.DataFrame.empty attribute you can check if DataFrame is empty or not. We refer DataFrame as empty when it has zero rows. This pandas DataFrame empty attribute returns a Boolean value; value True when DataFrame is empty and False when it is empty.

    We are often required to check whether panda DataFrame is empty for several reasons and wanted to ignore performing certain operations/actions when it is empty. In this article, I will cover few examples of how to check if DataFrame is empty or not with examples.

    Key Points –

    Notes:

    <a id="h-1-quick-examples-of-pandas-check-if-dataframe-is-empty"></a>

    1. Quick Examples of Pandas Check If DataFrame is Empty

    If you are in a hurry, below are some quick examples of pandas check if DataFrame is empty.

    
    # Check using empty attribute
    print(df_empty.empty) ==> Prints True
    
    # Check DataFrame Empty using shape()
    print(df_empty.shape[0] == 0) ==> Prints True
    
    # Check DataFrame Empty using size()
    print(df_empty.size == 0) ==> Prints True
    
    # Using len() Function
    print(len(df_empty) == 0) ==> Prints True
    
    # Using len() & index
    print(len(df_empty.index) == 0) ==> Prints True
    

    <a id="h-2-create-sample-dataframe-to-check-if-it-is-empty"></a>

    2. Create Sample DataFrame to Check If it is Empty

    In order to check if DataFrame is empty, let's create a DataFrame with empty using pandas.DataFrame() constructor and another DataFrame with few rows.

    
    import pandas as pd
    import numpy as np
    
    # This creates empty DataFrame
    df_empty=pd.DataFrame()
    
    # DataFrame with few rows
    df_non_empty=pd.DataFrame(['A','B','C'])
    

    <a id="h-3-check-dataframe-has-rows-using-empty-attribute"></a>

    3. Check DataFrame has Rows Using empty Attribute

    df_empty.empty return boolean value True as this DataFrame has no rows and df_non_empty.empty return False as it has 3 rows. In a real-time application, you would do something like below using if or similar conditional statements.

    
    # Check DataFrame is empty
    if(df_empty.empty == True):
        print('Empty DataFrame')
        # Do some thing when DF is empty
    else:
        print('Non Empty DataFrame')  
        # Proceed with regular operation
    

    Yields below output.

    
    # Output:
    Empty DataFrame
    

    <a id="h-4-using-dataframe-shape-amp-dataframe-size-to-check-if-it-is-empty"></a>

    4. Using DataFrame.shape() & DataFrame.size to check If it is Empty

    pandas.DataFrame.shape() method returns the number of rows and number of columns as a tuple, you can use this to check if pandas DataFrame is empty. DataFrame.shape[0] return number of rows. If you have no rows then it gives you 0 and comparing it with 0 gives you True.

    Similarly, you can also try size() method. size() returns a count of all cells (rows * columns).

    
    # Check DataFrame Empty using shape()
    print(df_empty.shape[0] == 0) == Prints True
    
    # Check DataFrame Empty using size()
    print(df_empty.size == 0) == Prints True
    

    <a id="h-5-check-whether-dataframe-is-empty-using-length-amp-index"></a>

    5. Check Whether DataFrame is empty Using Length & Index

    len(df) function gives a number of rows in DataFrame hence, you can use this to check whether DataFrame is empty.

    
    # Using len() Function
    print(len(df_empty) == 0) ==> Prints True
    

    But the best way to check if DataFrame is empty is using its Index. When there are no rows present, Dataframe.index returns an empty Series, and getting the length of it's gets you 0.

    
    # Using len() & index
    print(len(df_empty.index) == 0) ==> Prints True
    

    Note that len(DataFrame.index) gives you better performance compared with len(DataFrame) and DataFrame.empty attribute. For details check stackoverflow.com.

    <a id="h-6-checking-empty-dataframe-if-it-has-all-nan-or-none-values"></a>

    6. Checking Empty DataFrame If it has All NaN or None Values

    If you have a pandas DataFrame with all NaN/None values, checking if it is an empty return False. In order to get this as empty first, you need to drop all None/NaN values using dropna() method. Below I have provided examples.

    
    import numpy as np
    # DataFrame with all NaN values
    df_all_nan = pd.DataFrame([None,np.nan,np.NaN])
    
    print(df_all_nan.empty) ==> Returns False
    
    print(df_all_nan.dropna().empty) ==> Returns True
    

    <a id="h-7-complete-example"></a>

    7. Complete Example

    
    import pandas as pd
    import numpy as np
    
    # This creates empty DataFrame
    df_empty=pd.DataFrame()
    
    # DataFrame with few rows
    df_non_empty = pd.DataFrame(['A','B','C'])
    
    # Anotehr DataFrame with all NaN values
    df_all_nan = pd.DataFrame([np.nan,np.nan,np.NaN])
    
    # Check DataFrame is Empty using empty attribute
    print(df_all_nan.empty)
    print(df_empty.empty)
    
    # Check using empty attribute & if condition
    if(df_empty.empty == True):
        print('Empty DataFrame')
        # Do some thing when DF is empty
    else:
        print('Non Empty DataFrame')  
        # Proceed with regular operation
    
    # Check DF Empty using len() & index
    print(len(df_empty)==0)
    print(len(df_empty.index) ==0)
    
    # Check using share() & size
    print(df_empty.size == 0)
    print(df_empty.shape[0] == 0)
    
    # Check Empty if all values are None & NaN
    print(df_all_nan.empty)
    print(df_all_nan.dropna().empty)
    

    <a id="h-frequently-asked-questions-on-pandas-check-if-dataframe-is-empty"></a>

    Frequently Asked Questions on Pandas Check If DataFrame is Empty

    Use the empty attribute of the DataFrame. It returns True if the DataFrame is empty and False otherwise.

    The size property represents the total number of elements in the DataFrame. If the size is zero, the DataFrame is considered empty.

    A DataFrame with only missing values is not considered empty. The empty attribute and other methods focus on the presence of rows or columns, regardless of the values they contain.

    Using the empty attribute is often recommended due to its clarity and simplicity. It directly provides a boolean result indicating whether the DataFrame is empty.

    You can use the shape attribute and verify if either dimension is zero. Additionally, applying the len() function to the DataFrame and checking if the length is zero provides another approach.

    While there might be subtle performance variations, the differences are generally negligible. The empty attribute is often preferred for its simplicity, but choosing a method depends on the specific use case and personal preference.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to check whether a pandas DataFrame is empty using an empty attribute, size, share, and len methods. If your DataFrame contains only NaN/None values then it is not considered empty so df.empty attribute returns False. Note that len(DataFrame.index) gives you better performance compared with len(DataFrame) and DataFrame.empty attribute.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References