group_map
In [2]:
Copied!
# https://dplyr.tidyverse.org/reference/group_map.html
%run nb_helpers.py
from datar.data import mtcars, iris
from datar.all import *
nb_header(group_map)
# https://dplyr.tidyverse.org/reference/group_map.html
%run nb_helpers.py
from datar.data import mtcars, iris
from datar.all import *
nb_header(group_map)
Try this notebook on binder.
★ group_map¶
Apply a function to each group¶
The original API:
https://dplyr.tidyverse.org/reference/group_map.html
Args:¶
_data
: A grouped frame
_f
: A function to apply to each group.
*args
: Additional arguments to pass to func
.
_keep
: If True
, keep the grouping variables in the output.
**kwargs
: Additional keyword arguments to pass to func
.
Returns:¶
A list of results
In [3]:
Copied!
list(
mtcars >> \
group_by(f.cyl) >> \
group_map(lambda df: df >> head(2))
)
list(
mtcars >> \
group_by(f.cyl) >> \
group_map(lambda df: df >> head(2))
)
Out[3]:
[ mpg disp hp drat ... vs am gear carb <float64> <float64> <int64> <float64> ... <int64> <int64> <int64> <int64> 0 21.0 160.0 110 3.9 ... 0 1 4 4 1 21.0 160.0 110 3.9 0 1 4 4 [2 rows x 10 columns], mpg disp hp drat ... vs am gear carb <float64> <float64> <int64> <float64> ... <int64> <int64> <int64> <int64> 0 22.8 108.0 93 3.85 ... 1 1 4 1 1 24.4 146.7 62 3.69 1 0 4 2 [2 rows x 10 columns], mpg disp hp drat ... vs am gear carb <float64> <float64> <int64> <float64> ... <int64> <int64> <int64> <int64> 0 18.7 360.0 175 3.15 ... 0 0 3 2 1 14.3 360.0 245 3.21 0 0 3 4 [2 rows x 10 columns]]
In [4]:
Copied!
mtcars >> \
group_by(f.cyl) >> \
group_modify(lambda df: df >> head(2))
mtcars >> \
group_by(f.cyl) >> \
group_modify(lambda df: df >> head(2))
Out[4]:
cyl | mpg | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
<int64> | <float64> | <float64> | <int64> | <float64> | <float64> | <float64> | <int64> | <int64> | <int64> | <int64> | |
0 | 6 | 21.0 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
1 | 6 | 21.0 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
0 | 4 | 22.8 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
1 | 4 | 24.4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 | 0 | 4 | 2 |
0 | 8 | 18.7 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
1 | 8 | 14.3 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 | 0 | 3 | 4 |
TibbleGrouped: cyl (n=3)
In [5]:
Copied!
list(
iris >> \
group_by(f.Species) >> \
group_map(lambda df: quantile(df['Petal_Length'], probs=c(0.25, 0.5, 0.75)))
)
list(
iris >> \
group_by(f.Species) >> \
group_map(lambda df: quantile(df['Petal_Length'], probs=c(0.25, 0.5, 0.75)))
)
Out[5]:
[array([1.4 , 1.5 , 1.575]), array([4. , 4.35, 4.6 ]), array([5.1 , 5.55 , 5.875])]
In [6]:
Copied!
iris >> \
group_by(f.Species) >> \
group_walk(lambda df: print(df.shape))
iris >> \
group_by(f.Species) >> \
group_walk(lambda df: print(df.shape))
(50, 4) (50, 4) (50, 4)
In [7]:
Copied!
mtcars >> \
group_modify(lambda df: df >> head(2))
mtcars >> \
group_modify(lambda df: df >> head(2))
Out[7]:
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
<float64> | <int64> | <float64> | <int64> | <float64> | <float64> | <float64> | <int64> | <int64> | <int64> | <int64> | |
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
In [ ]:
Copied!