0

I am trying to replace Pandas with Polars in my code. I have one line code with pandas that returns a series where the ith element is a boolean that indicates if the ith row in df has missing values, see below

df.isnull().any(axis=1)

My current way to do this with polars is

df.select(pl.any([pl.col(c).isnull() for c in df.columns]))

which seems to be more complicated than the pandas code, I wonder if there is a simpler and more elegant way to do this.

jj2523
  • 1
  • 1

1 Answers1

2

As of polars>=0.13.59 you can pass expressions as arguments to all/any.

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

df.select([
    pl.any(pl.all().is_null()).alias("null_in_row"),
])
shape: (3, 1)
┌─────────────┐
│ null_in_row │
│ ---         │
│ bool        │
╞═════════════╡
│ false       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ true        │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ true        │
└─────────────┘
ritchie46
  • 10,405
  • 1
  • 24
  • 43