[Python] Following and Count of State Changes Between Columns in Polars

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with multiple IDs and corresponding states. I want to analyze how the states have changed over time and present this information effectively.

    Here is an example:

    import polars as pl

    df = pl.DataFrame({
    "ID": [1, 2, 3],
    "T0": ["A", "B", "C"],
    "T1": ["B", "B", "A"],
    })


    One aproach it's to "concat" the columns , and then do a value_counts() of the Change column

    df = df.with_columns(
    (pl.col("T0") + " -> " + pl.col("T1")).alias("Change")
    )


    However, there might be a better approach to this, or even a built-in function that can achieve what I need more efficiently.

    Current Output:

    shape: (3, 4)
    ┌─────┬─────┬─────┬────────┐
    │ ID ┆ T0 ┆ T1 ┆ Change │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ i64 ┆ str ┆ str ┆ str │
    ╞═════╪═════╪═════╪════════╡
    │ 1 ┆ A ┆ B ┆ A -> B │
    │ 2 ┆ B ┆ B ┆ B -> B │
    │ 3 ┆ C ┆ A ┆ C -> A │
    └─────┴─────┴─────┴────────┘

    shape: (3, 2)
    ┌────────┬───────┐
    │ Change ┆ count │
    │ --- ┆ --- │
    │ str ┆ u32 │
    ╞════════╪═══════╡
    │ C -> A ┆ 1 │
    │ B -> B ┆ 1 │
    │ A -> B ┆ 1 │
    └────────┴───────┘

    Continue reading...

Compartilhe esta Página