我有这个示例 Polars DataFrame:
import polars as pl
df = pl.DataFrame({
"id": [1, 2, 3, 4, 5],
"variable1": [15, None, 5, 10, 20],
"variable2": [40, 30, 50, 10, None],
})
我尝试使用方法过滤数据框的所有列pl.all()
,也尝试使用pl.any_horizontal() == Condition
。但是我收到以下错误:
ComputeError: The predicate passed to 'LazyFrame.filter' expanded to multiple expressions:
col("id").is_not_null(),
col("variable1").is_not_null(),
col("variable2").is_not_null(),
This is ambiguous. Try to combine the predicates with the 'all' or `any' expression.
以下是我尝试解决这个问题的尝试。
# Attempt 1:
(
df
.filter(
pl.all().is_not_null()
)
)
# Attempt 2:
(
df
.filter(
pl.any_horizontal().is_not_null()
)
)
所需的输出,但它不适用于更大的 DataFrames:
(
df
.filter(
pl.col("variable1").is_not_null(),
pl.col("variable2").is_not_null()
)
)
如何以可扩展的方式过滤所有列而无需单独指定每一列?