Nil*_*age 249 python dataframe pandas
如何检查大熊猫是否DataFrame
为空?在我的情况下,我想在终端打印一些消息,如果它DataFrame
是空的.
aIK*_*Kid 390
您可以使用该属性df.empty
检查它是否为空:
if df.empty:
print('DataFrame is empty!')
Run Code Online (Sandbox Code Playgroud)
来源:熊猫文档
Zer*_*ero 45
我使用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
'''
Run Code Online (Sandbox Code Playgroud)
Sve*_*ile 14
要查看数据框是否为空,我认为应该测试数据框列索引的长度:
if len(df.columns) == 0: 1
Run Code Online (Sandbox Code Playgroud)
根据Pandas Reference API,有以下区别:
NaN
因此至少1列可以说,它们并不相同。其他答案不精确,因为df.empty
,len(df)
或len(df.index)
没有区别并且返回索引为 0并且在这两种情况下空为 True。
示例 1:具有 0 行和 0 列的空数据框
In [1]: import pandas as pd
df1 = pd.DataFrame()
df1
Out[1]: Empty DataFrame
Columns: []
Index: []
In [2]: len(df1.index) # or len(df1)
Out[2]: 0
In [3]: df1.empty
Out[3]: True
Run Code Online (Sandbox Code Playgroud)
示例 2:清空为 0 行但仍保留n
列的数据框
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
df2
Out[4]: AA BB
0 1 11
1 2 22
2 3 33
In [5]: df2 = df2[df2['AA'] == 5]
df2
Out[5]: Empty DataFrame
Columns: [AA, BB]
Index: []
In [6]: len(df2.index) # or len(df2)
Out[6]: 0
In [7]: df2.empty
Out[7]: True
Run Code Online (Sandbox Code Playgroud)
现在,以前面的示例为基础,其中索引为 0,空为 True。在读取第一个加载的数据帧 df1的列索引长度时,它返回 0 列以证明它确实为空。
In [8]: len(df1.columns)
Out[8]: 0
In [9]: len(df2.columns)
Out[9]: 2
Run Code Online (Sandbox Code Playgroud)
关键的是,虽然第二个数据帧 df2 不包含任何数据,但它并不是完全空的,因为它返回持续存在的空列的数量。
让我们向这些数据框添加一个新列以了解其含义:
# As expected, the empty column displays 1 series
In [10]: df1['CC'] = [111, 222, 333]
df1
Out[10]: CC
0 111
1 222
2 333
In [11]: len(df1.columns)
Out[11]: 1
# Note the persisting series with rows containing `NaN` values in df2
In [12]: df2['CC'] = [111, 222, 333]
df2
Out[12]: AA BB CC
0 NaN NaN 111
1 NaN NaN 222
2 NaN NaN 333
In [13]: len(df2.columns)
Out[13]: 3
Run Code Online (Sandbox Code Playgroud)
很明显,df2 中的原始列已经重新浮出水面。因此,为谨慎起见,而不是读出的列索引的长度与len(pandas.core.frame.DataFrame.columns)
以查看是否有数据帧是空的。
# New dataframe df
In [1]: df = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
df
Out[1]: AA BB
0 1 11
1 2 22
2 3 33
# This data manipulation approach results in an empty df
# because of a subset of values that are not available (`NaN`)
In [2]: df = df[df['AA'] == 5]
df
Out[2]: Empty DataFrame
Columns: [AA, BB]
Index: []
# NOTE: the df is empty, BUT the columns are persistent
In [3]: len(df.columns)
Out[3]: 2
# And accordingly, the other answers on this page
In [4]: len(df.index) # or len(df)
Out[4]: 0
In [5]: df.empty
Out[5]: True
Run Code Online (Sandbox Code Playgroud)
# SOLUTION: conditionally check for empty columns
In [6]: if len(df.columns) != 0: # <--- here
# Do something, e.g.
# drop any columns containing rows with `NaN`
# to make the df really empty
df = df.dropna(how='all', axis=1)
df
Out[6]: Empty DataFrame
Columns: []
Index: []
# Testing shows it is indeed empty now
In [7]: len(df.columns)
Out[7]: 0
Run Code Online (Sandbox Code Playgroud)
添加新数据系列按预期工作,无需重新显示空列(实际上,没有任何包含仅包含 的行的系列NaN
):
In [8]: df['CC'] = [111, 222, 333]
df
Out[8]: CC
0 111
1 222
2 333
In [9]: len(df.columns)
Out[9]: 1
Run Code Online (Sandbox Code Playgroud)
fix*_*xer 10
我更喜欢走很长的路.这些是我为避免使用try-except子句而遵循的检查 -
在这里,DATA
是可疑变量 -
DATA is not None and isinstance(DATA, pd.DataFrame) and not DATA.empty
Run Code Online (Sandbox Code Playgroud)