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
How to check whether a pandas DataFrame is empty?
提问by Nilani Algiriyage
How to check whether a pandas DataFrame
is empty? In my case I want to print some message in terminal if the DataFrame
is empty.
如何检查熊猫是否DataFrame
为空?在我的情况下,如果DataFrame
为空,我想在终端中打印一些消息。
采纳答案by aIKid
You can use the attribute df.empty
to 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 len
function. It's much faster than empty
. len(df.index)
is even faster.
我用这个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
'''
回答by fixxxer
I prefer going the long route. These are the checks I follow to avoid using a try-except clause -
我更喜欢走长路。这些是我为了避免使用 try-except 子句而遵循的检查 -
- check if variable is not None
- then check if its a dataframe and
- make sure its not empty
- 检查变量是否不是 None
- 然后检查它是否是一个数据框和
- 确保它不是空的
Here, DATA
is 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')