我想检查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
我试过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().
好像它甚至没有注意到我使用了.bool()
然后我使用a.empty
并回答了我:
AttributeError: 'list' object has no attribute 'empty'
答案 0 :(得分:5)
IIUC:有.empty
属性:
数据帧:
In [86]: pd.DataFrame().empty
Out[86]: True
In [87]: pd.DataFrame([1,2,3]).empty
Out[87]: False
系列:
In [88]: pd.Series().empty
Out[88]: True
In [89]: pd.Series([1,2,3]).empty
Out[89]: False
注意:与len(df)
方法相比,检查DF的长度(df.empty
)可能会节省几毫秒; - )
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)
答案 1 :(得分:1)
或者您可以使用.shape
pd.DataFrame().shape
Out[1666]: (0, 0)
pd.DataFrame([1,2,3]).shape
Out[1667]: (3, 1)