Reputation: 1634
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 │
└─────────┴──────────┘
Upvotes: 0
Views: 2307
Reputation: 14710
df.select(
pl.col("rebounds").filter((pl.col("points") >= 8) & (pl.col("team") != 'B')).sum()
)
shape: (1, 1)
┌──────────┐
│ rebounds │
│ --- │
│ i64 │
╞══════════╡
│ 20 │
└──────────┘
Upvotes: 2