Python pandas check if dataframe is not empty

前端 未结 4 2207
借酒劲吻你
借酒劲吻你 2021-02-11 11:55

I have an if statement where it checks if the data frame is not empty. The way I do it is the following:

if dataframe.empty:
    pass
else:
    #do          


        
相关标签:
4条回答
  • 2021-02-11 12:27

    .empty returns a boolean value

    >>> df_empty.empty
    True
    

    So if not empty can be written as

    if not df.empty:
        #Your code
    

    Check pandas.DataFrame.empty , might help someone.

    0 讨论(0)
  • 2021-02-11 12:36

    Just do

    if not dataframe.empty:
         # insert code here
    

    The reason this works is because dataframe.empty returns True if dataframe is empty. To invert this, we can use the negation operator not, which flips True to False and vice-versa.

    0 讨论(0)
  • 2021-02-11 12:37

    Another way:

    if dataframe.empty == False:
        #do something`
    
    0 讨论(0)
  • 2021-02-11 12:46

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

    if not dataframe.empty:
        #do something
    

    Or

    if len(dataframe) != 0:
       #do something
    

    Or

    if len(dataframe.index) != 0:
       #do something
    
    0 讨论(0)
提交回复
热议问题