Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does polars support columns selection using boolean type?

I want to select those columns whose all rows value is null. In pandas, I can use:

nan_columns = specific_data.columns[specific_data.isna().all()]

but I found its logic may be a little hard for polars. I found it:

 nan_columns = specific_data[:, specific_data.select(pl.all().is_null().all()).row(0)].columns[0]

Am I wrong? Or is there better solution for it?

like image 899
ChenHuang Avatar asked Sep 02 '26 01:09

ChenHuang


1 Answers

Note. Please take a look at this polars discussion page to better understand the intricacies of data-dependant column selection and implications on lazy/eager evaluation.

Lets consider a dataframe defined as

import polars as pl

df = pl.DataFrame({
    "a": [1, 2, 3],
    "b": [None, None, None],
    "c": [True, False, True],
})

which looks as follows

shape: (3, 3)
┌─────┬──────┬───────┐
│ a   ┆ b    ┆ c     │
│ --- ┆ ---  ┆ ---   │
│ i64 ┆ f32  ┆ bool  │
╞═════╪══════╪═══════╡
│ 1   ┆ null ┆ true  │
│ 2   ┆ null ┆ false │
│ 3   ┆ null ┆ true  │
└─────┴──────┴───────┘

You could now use

df.select(col for col in df if col.is_null().all())

or

df.select(col.name for col in df.select(pl.all().is_null().all()) if col.item())

The latter will ensure the inner conditions are computed in parallel.

like image 196
Hericks Avatar answered Sep 05 '26 16:09

Hericks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!