pandas 如何检查DataFrame是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45802101/
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 DataFrame is empty?
提问by Antoine Coppin
I want to check whether a DataFrame is empty :
我想检查一个 DataFrame 是否为空:
BTC_ewma_24 ETH_ewma_24 DASH_ewma_24
24 4011.235578 334.597119 281.15
25 4011.285662 334.591056 281.15
26 4011.373673 334.603479 281.15
27 4011.453068 334.614686 281.15
28 4011.526571 334.624813 281.15
29 4011.591356 334.633980 281.15
30 4011.650075 334.642288 281.15
31 4011.703366 334.649828 281.15
I tried if(self.mean_exp.bool() == False):
but it answers me :
我试过了,if(self.mean_exp.bool() == False):
但它回答了我:
ValueError: The truth value of a DataFrame is ambiguous.
Use a.empty, a.bool(), a.item(), a.any() or a.all().
As if it didn't even noticed that I used .bool()
好像它甚至没有注意到我用过 .bool()
I then used a.empty
and it answered me :
然后我使用a.empty
它回答了我:
AttributeError: 'list' object has no attribute 'empty'
回答by MaxU
IIUC: there is .empty
attribute:
IIUC:有.empty
属性:
DataFrame:
数据框:
In [86]: pd.DataFrame().empty
Out[86]: True
In [87]: pd.DataFrame([1,2,3]).empty
Out[87]: False
Series:
系列:
In [88]: pd.Series().empty
Out[88]: True
In [89]: pd.Series([1,2,3]).empty
Out[89]: False
NOTE: checking the length of DF (len(df)
) might save you a few milliseconds compared to df.empty
method ;-)
注意:len(df)
与df.empty
方法相比,检查 DF ( )的长度可能会为您节省几毫秒;-)
In [142]: df = pd.DataFrame()
In [143]: %timeit df.empty
8.25 μs ± 22.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [144]: %timeit len(df)
2.35 μs ± 7.56 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [145]: df = pd.DataFrame(np.random.randn(10*5, 3), columns=['a', 'b', 'c'])
In [146]: %timeit df.empty
15.3 μs ± 269 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [147]: %timeit len(df)
3.58 μs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
回答by YOBEN_S
Or you can using .shape
或者你可以使用 .shape
pd.DataFrame().shape
Out[1666]: (0, 0)
pd.DataFrame([1,2,3]).shape
Out[1667]: (3, 1)