Polars - 检查数据框中是否为空

问题描述 投票:0回答:1

我知道我可以在 Polars 中执行

.null_count()
,它会返回一个数据帧,告诉我每列的空计数。

d = {"foo":[1,2,3, None], "bar":[4,None, None, 6]}
df_polars_withnull = pl.from_dict(d)

df_polars_withnull.null_count()

会产生一个数据框

foo   bar
  1     2

我想知道整个数据框中是否有任何空值

类似的东西

if any(df_polars_withnull.null_count()):
   print ('has nulls')
else:
   print ('no nulls')

不幸的是,这不起作用。这里正确的代码是什么?

这可行,但看起来有点难看

if df_polars_nonull.null_count().sum(axis=1)[0]:
   print ('has nulls')
else:
   print ('no nulls')

python python-polars
1个回答
0
投票

如何使用

melt
重塑为单列,然后运行
is_null
+
any
:

df_polars_withnull.melt()['value'].is_null().any()

输出:

True

© www.soinside.com 2019 - 2024. All rights reserved.