如何检查pandas DataFrame是否为空?

时间:2013-11-07 05:45:36

标签: python pandas dataframe

如何检查pandas DataFrame是否为空?在我的情况下,如果DataFrame为空,我想在终端中打印一些消息。

5 个答案:

答案 0 :(得分:338)

您可以使用属性df.empty检查它是否为空:

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

来源:Pandas Documentation

答案 1 :(得分:39)

我使用len功能。它比empty()快得多。 len(df.index)甚至更快。

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

'''
%timeit empty(df)
%timeit lenz(df)
%timeit lenzi(df)

10000 loops, best of 3: 13.9 µs per loop
100000 loops, best of 3: 2.34 µs per loop
1000000 loops, best of 3: 695 ns per loop

len on index seems to be faster
'''

答案 2 :(得分:10)

我更喜欢漫长的路线。这些是我遵循的检查,以避免使用try-except子句 -

  1. 检查变量是否不是
  2. 然后检查它是否是数据框和
  3. 确保它不是空的
  4. 此处,DATA是可疑变量 -

    DATA is not None and isinstance(DATA, pd.DataFrame) and not DATA.empty
    

答案 3 :(得分:2)

看来,此线程中接受的空定义是只有零行的数据框。但是在具有零行零列的空数据帧与具有零行且至少一列的空数据帧之间存在区别。在每种情况下,索引的长度均为0,并且空= True,如下所示:

示例1:具有零行零列的空数据框

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)
Out[2]: 0

In [3]: df1.empty
Out[3]: True

示例2:具有零行和至少一列的空数据框

In [4]: df2 = pd.DataFrame({'AA' : [], 'BB' : []})
        df2
Out[4]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [5]: len(df2.index)
Out[5]: 0

In [6]: df2.empty
Out[6]: True

区分没有标题和数据的数据框或只是没有数据的数据框的一种方法是测试列索引的长度。第一个加载的数据帧返回零列,第二个返回的列数。

In [7]: len(df1.columns)
Out[7]: 0

In [8]: len(df2.columns)
Out[8]: 2

答案 4 :(得分:0)

1) If a DataFrame has got Nan and Non Null values and you want to find whether the DataFrame
is empty or not then try this code.
2) when this situation can happen? 
This situation happens when a single function is used to plot more than one DataFrame 
which are passed as parameter.In such a situation the function try to plot the data even 
when a DataFrame is empty and thus plot an empty figure!.
It will make sense if simply display 'DataFrame has no data' message.
3) why? 
if a DataFrame is empty(i.e. contain no data at all.Mind you DataFrame with Nan values 
is considered non empty) then it is desirable not to plot but put out a message :
Suppose we have two DataFrames df1 and df2.
The function myfunc takes any DataFrame(df1 and df2 in this case) and print a message 
if a DataFrame is empty(instead of plotting):
df1                     df2
col1 col2           col1 col2 
Nan   2              Nan  Nan 
2     Nan            Nan  Nan  

和功能:

def myfunc(df):
  if (df.count().sum())>0: ##count the total number of non Nan values.Equal to 0 if DataFrame is empty
     print('not empty')
     df.plot(kind='barh')
  else:
     display a message instead of plotting if it is empty
     print('empty')