otsaloma / dataiter

Python classes for data manipulation
https://dataiter.readthedocs.io/
MIT License
25 stars 0 forks source link

Add grouped modify #19

Closed otsaw closed 2 years ago

otsaw commented 2 years ago

Usually used to calculate fractions.

Like in dplyr

> data.frame(x=rep(1:3, 3), y=1) %>% group_by(x) %>% mutate(z=1/sum(y))
  x y        z
1 1 1 0.333333
2 2 1 0.333333
3 3 1 0.333333
4 1 1 0.333333
5 2 1 0.333333
6 3 1 0.333333
7 1 1 0.333333
8 2 1 0.333333
9 3 1 0.333333
otsaloma commented 2 years ago
>>> (di.DataFrame(x=np.repeat([1, 2, 3], 3), y=1)
... .group_by("x")
... .modify(z=lambda d: 1 / sum(d.y)))

      x     y        z
  int64 int64  float64
  ───── ───── ────────
0     1     1 0.333333
1     1     1 0.333333
2     1     1 0.333333
3     2     1 0.333333
4     2     1 0.333333
5     2     1 0.333333
6     3     1 0.333333
7     3     1 0.333333
8     3     1 0.333333