1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Conditional sum by columns in Polars Python

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 19:33.

  1. Stack

    Stack Membro Participativo

    I understand how to perform conditional sum in columns but I am wondering how to achieve a similar approach and end up as a dataframe

    import pandas as pd

    df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'C'],
    'conference': ['East', 'East', 'East', 'West', 'West', 'East'],
    'points': [11, 8, 10, 6, 6, 5],
    'rebounds': [7, 7, 6, 9, 12, 8]})

    pl.from_pandas(df)


    ┌──────┬────────────┬────────┬──────────┐
    │ team ┆ conference ┆ points ┆ rebounds │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ str ┆ str ┆ i64 ┆ i64 │
    ╞══════╪════════════╪════════╪══════════╡
    │ A ┆ East ┆ 11 ┆ 7 │
    │ A ┆ East ┆ 8 ┆ 7 │
    │ A ┆ East ┆ 10 ┆ 6 │
    │ B ┆ West ┆ 6 ┆ 9 │
    │ B ┆ West ┆ 6 ┆ 12 │
    │ C ┆ East ┆ 5 ┆ 8 │
    └──────┴────────────┴────────┴──────────┘


    Pandas solution:

    df.loc[(df2['points'] >= 8) & (df['team'] != 8), 'rebounds'].sum()
    df.query("points >= 8 and team != 'B' ")['rebounds'].sum()


    Result:

    20
    ┌─────────┬──────────┐
    │ column ┆ column_0 │
    │ --- ┆ --- │
    │ str ┆ u32 │
    ╞═════════╪══════════╡
    │ group_a ┆ 20 │
    │ group_b ┆ 10 │
    └─────────┴──────────┘

    Continue reading...

Compartilhe esta Página