I want to groupby some columns, but one of the columns is either 'Fire' or 'Water'. I need to count the occurences of 'Fire' and 'Water' in seperate columns and also have a 'total' column which counts the sum of 'Fire' and 'Water'
Example:
df = pl.DataFrame({'ID': [0,0,1, 1, 1,0], 'Type': ['Fire', 'Fire', 'Fire', 'Water', 'Water', 'Water'], })
>>>df
shape: (6, 2)
┌─────┬───────┐
│ ID ┆ Tye
│ i64 ┆ str │
│ 0 ┆ Fire │
│ 0 ┆ Fire │
│ 1 ┆ Fire │
│ 1 ┆ Water │
│ 1 ┆ Water │
│ 0 ┆ Water │
df = df.groupby(['ID', 'Type']).agg(pl.count())
shape: (4, 3)
┌─────┬───────┬───────┐
│ ID ┆ Type ┆ count │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ u32 │
╞═════╪═══════╪═══════╡
│ 1 ┆ Fire ┆ 1 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1 ┆ Water ┆ 2 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 0 ┆ Fire ┆ 2 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 0 ┆ Water ┆ 1 │
└─────┴───────┴───────┘
What i would rather love to have is:
┌─────┬───────┬──────┬───────┐
│ ID ┆ water ┆ fire ┆ total │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ u32 ┆ u32 ┆ u32 │
╞═════╪═══════╪══════╪═══════╡
│ 1 ┆ 2 ┆ 1 ┆ 3 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 0 ┆ 1 ┆ 2 ┆ 3 │
└─────┴───────┴──────┴───────┘
How would i do that?