Python Pandas检查数据框是否不为空 [英] Python pandas check if dataframe is not empty
问题描述
我有一个if
语句,它在其中检查数据帧是否不为空.我的操作方式如下:
I have an if
statement where it checks if the data frame is not empty. The way I do it is the following:
if dataframe.empty:
pass
else:
#do something
但是我确实需要:
if dataframe is not empty:
#do something
我的问题是-是否有方法.not_empty()
来实现这一目标?我还想问一下第二个版本在性能方面是否更好?否则,也许我应该保留它的原样,即第一个版本?
My question is - is there a method .not_empty()
to achieve this? I also wanted to ask if the second version is better in terms of performance? Otherwise maybe it makes sense for me to leave it as it is i.e. the first version?
推荐答案
只要做
if not dataframe.empty:
# insert code here
之所以起作用,是因为如果数据帧为空,则dataframe.empty
返回True
.为了反过来,我们可以使用求反运算符not
,它将True
翻转为False
,反之亦然.
The reason this works is because dataframe.empty
returns True
if dataframe is empty. To invert this, we can use the negation operator not
, which flips True
to False
and vice-versa.
这篇关于Python Pandas检查数据框是否不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!