[Python] Removing null values on selected columns only in Polars dataframe

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to remove null values across a list of selected columns. But it seems that I might have got the with_columns operation not right. What's the right approach if you want to operate the removing only on selected columns?

    df = pl.DataFrame(
    {
    "id": ["NY", "TK", "FD"],
    "eat2000": [1, None, 3],
    "eat2001": [-2, None, 4],
    "eat2002": [None, None, None],
    "eat2003": [-9, None, 8],
    "eat2004": [None, None, 8]
    }
    ); df


    ┌─────┬─────────┬─────────┬─────────┬─────────┬─────────┐
    │ id ┆ eat2000 ┆ eat2001 ┆ eat2002 ┆ eat2003 ┆ eat2004 │
    │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
    │ str ┆ i64 ┆ i64 ┆ f64 ┆ i64 ┆ i64 │
    ╞═════╪═════════╪═════════╪═════════╪═════════╪═════════╡
    │ NY ┆ 1 ┆ -2 ┆ null ┆ -9 ┆ null │
    │ TK ┆ null ┆ null ┆ null ┆ null ┆ null │
    │ FD ┆ 3 ┆ 4 ┆ null ┆ 8 ┆ 8 │
    └─────┴─────────┴─────────┴─────────┴─────────┴─────────┘


    col_list = [word for word in df.columns if word.startswith(("eat"))]
    (
    df
    .with_columns(
    pl.col(col_list).filter(~pl.fold(True, lambda acc, s: acc & s.is_null(), pl.all()))
    )
    )


    # InvalidOperationError: dtype String not supported in 'not' operation


    Expected output:

    ┌─────┬─────────┬─────────┬─────────┬─────────┬─────────┐
    │ id ┆ eat2000 ┆ eat2001 ┆ eat2002 ┆ eat2003 ┆ eat2004 │
    │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
    │ str ┆ i64 ┆ i64 ┆ f64 ┆ i64 ┆ i64 │
    ╞═════╪═════════╪═════════╪═════════╪═════════╪═════════╡
    │ NY ┆ 1 ┆ -2 ┆ null ┆ -9 ┆ null │
    │ FD ┆ 3 ┆ 4 ┆ null ┆ 8 ┆ 8 │
    └─────┴─────────┴─────────┴─────────┴─────────┴─────────┘

    Continue reading...

Compartilhe esta Página