Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiply within a group in polars

I would like to take the product within a group in polars

The following works but I suspect there is a more elegant/efficient way to perform this operation. Thank you

import polars as pl
import numpy as np

D = pl.DataFrame({'g':['a','a','b','b'],'v':[1,2,3,4],'v2':[2,3,4,5]})
D.group_by('g').agg(pl.all().map_elements(
                        lambda group: np.prod(group.to_numpy()),return_dtype=pl.Float64))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use Expr.product:

D.group_by('g').agg(pl.all().product())

Output:

┌─────┬─────┬─────┐
│ g   ┆ v   ┆ v2  │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ b   ┆ 12  ┆ 20  │
│ a   ┆ 2   ┆ 6   │
└─────┴─────┴─────┘

If you want Floats:

D.group_by('g').agg(pl.all().product().cast(pl.Float64))

┌─────┬──────┬──────┐
│ g   ┆ v    ┆ v2   │
│ --- ┆ ---  ┆ ---  │
│ str ┆ f64  ┆ f64  │
╞═════╪══════╪══════╡
│ b   ┆ 12.0 ┆ 20.0 │
│ a   ┆ 2.0  ┆ 6.0  │
└─────┴──────┴──────┘
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading