Python 如何检查 Pandas DataFrame 是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19828822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:44:35  来源:igfitidea点击:

How to check whether a pandas DataFrame is empty?

pythonpandasdataframe

提问by Nilani Algiriyage

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

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

采纳答案by aIKid

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

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

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

Source: Pandas Documentation

来源:Pandas 文档

回答by Zero

I use the lenfunction. It's much faster than empty. len(df.index)is even faster.

我用这个len功能。它比 快得多emptylen(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
'''

回答by fixxxer

I prefer going the long route. These are the checks I follow to avoid using a try-except clause -

我更喜欢走长路。这些是我为了避免使用 try-except 子句而遵循的检查 -

  1. check if variable is not None
  2. then check if its a dataframe and
  3. make sure its not empty
  1. 检查变量是否不是 None
  2. 然后检查它是否是一个数据框和
  3. 确保它不是空的

Here, DATAis the suspect variable -

DATA是可疑的变量 -

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

回答by Sven Haile

It appears that the accepted the definition of empty in this thread is a dataframe with zero rows only. But there is a distinction between an empty dataframe with zero rows and zero columnsand an empty dataframe with zero rows and at least one column. In each case the length of the index is 0 and empty = True as shown here:

似乎在这个线程中接受的空定义是一个只有零行的数据帧。但是,具有零行和零列空数据帧与具有零行和至少一列空数据帧之间存在区别。在每种情况下,索引的长度都是 0 和 empty = True,如下所示:

Example 1: empty dataframe with 0 rows and 0 columns

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

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

Example 2: empty dataframe with 0 rows and at least 1 column

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

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

One way to distinguish between a dataframe that is empty of headers and dataor just empty of datais to test the length of the column index. The first loaded dataframe returns zero columns, the second dataframe returns the number of empty columns.

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

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

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

回答by Gul Saeed Khattak

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  

and the function:

和功能:

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')