[Python] Selecting columns based on a condition in Polars

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I want to select columns in a Polars DataFrame based on a condition. In my case, I want to select all string columns that have less than 100 unique values. Naively I tried:

    df.select((pl.col(pl.String)) & (pl.all().n_unique() < 100))


    which gave me an error, which is probably due to the second part of the expression.

    df.select(pl.all().n_unique() < 100)


    This doesn't select columns but instead returns a single row DataFrame of bool values. I'm new to polars and still can't quite wrap my head around the expression API, I guess. What am I doing wrong?

    Continue reading...

Compartilhe esta Página