[英]Pandas - check if ALL values are NaN in Series
I have a data series which looks like this:我有一个如下所示的数据系列:
print mys
id_L1
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
I would like to check is all the values are NaN.我想检查所有值是否都是 NaN。
My attempt:我的尝试:
pd.isnull(mys).all()
Output:输出:
True
Is this the correct way to do it?这是正确的方法吗?
是的,这是正确的,但我认为更惯用的方式是:
mys.isnull().all()
这将检查所有列..
mys.isnull().values.all(axis=0)
if df['col'].count() > 0:
then ...
This works well but I think it might be quite a slow approach.这很有效,但我认为这可能是一种很慢的方法。 I made the mistake of embedding this into a 6000-times loop to test four columns - and it's brutal, but I can blame the programmer clearly :)
我错误地将其嵌入到 6000 次循环中以测试四列 - 这很残酷,但我可以清楚地责怪程序员:)
Obviously, don't be like me.显然,不要像我一样。 Always: Test your columns for all-null once, set a variable with the yes - "empty" or no - "not empty" result - and then loop.
始终:测试您的列是否为全空一次,使用 yes -“空”或否 - “非空”结果设置一个变量 - 然后循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.