group_split
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/group_split.html
%run nb_helpers.py
from datar.data import iris
from datar.all import *
nb_header(group_split)
# https://dplyr.tidyverse.org/reference/group_split.html
%run nb_helpers.py
from datar.data import iris
from datar.all import *
nb_header(group_split)
Try this notebook on binder.
★ group_split¶
Split a grouped frame into a list of data frames¶
The original API:
https://dplyr.tidyverse.org/reference/group_split.html
Args:¶
_data
: A grouped frame
*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 data frames
In [2]:
Copied!
ir = iris >> group_by(f.Species)
list(group_split(ir))
ir = iris >> group_by(f.Species)
list(group_split(ir))
Out[2]:
[ Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa 5 5.4 3.9 1.7 0.4 setosa 6 4.6 3.4 1.4 0.3 setosa 7 5.0 3.4 1.5 0.2 setosa 8 4.4 2.9 1.4 0.2 setosa 9 4.9 3.1 1.5 0.1 setosa 10 5.4 3.7 1.5 0.2 setosa 11 4.8 3.4 1.6 0.2 setosa 12 4.8 3.0 1.4 0.1 setosa 13 4.3 3.0 1.1 0.1 setosa 14 5.8 4.0 1.2 0.2 setosa 15 5.7 4.4 1.5 0.4 setosa 16 5.4 3.9 1.3 0.4 setosa 17 5.1 3.5 1.4 0.3 setosa 18 5.7 3.8 1.7 0.3 setosa 19 5.1 3.8 1.5 0.3 setosa 20 5.4 3.4 1.7 0.2 setosa 21 5.1 3.7 1.5 0.4 setosa 22 4.6 3.6 1.0 0.2 setosa 23 5.1 3.3 1.7 0.5 setosa 24 4.8 3.4 1.9 0.2 setosa 25 5.0 3.0 1.6 0.2 setosa 26 5.0 3.4 1.6 0.4 setosa 27 5.2 3.5 1.5 0.2 setosa 28 5.2 3.4 1.4 0.2 setosa 29 4.7 3.2 1.6 0.2 setosa 30 4.8 3.1 1.6 0.2 setosa 31 5.4 3.4 1.5 0.4 setosa 32 5.2 4.1 1.5 0.1 setosa 33 5.5 4.2 1.4 0.2 setosa 34 4.9 3.1 1.5 0.2 setosa 35 5.0 3.2 1.2 0.2 setosa 36 5.5 3.5 1.3 0.2 setosa 37 4.9 3.6 1.4 0.1 setosa 38 4.4 3.0 1.3 0.2 setosa 39 5.1 3.4 1.5 0.2 setosa 40 5.0 3.5 1.3 0.3 setosa 41 4.5 2.3 1.3 0.3 setosa 42 4.4 3.2 1.3 0.2 setosa 43 5.0 3.5 1.6 0.6 setosa 44 5.1 3.8 1.9 0.4 setosa 45 4.8 3.0 1.4 0.3 setosa 46 5.1 3.8 1.6 0.2 setosa 47 4.6 3.2 1.4 0.2 setosa 48 5.3 3.7 1.5 0.2 setosa 49 5.0 3.3 1.4 0.2 setosa, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 7.0 3.2 4.7 1.4 versicolor 1 6.4 3.2 4.5 1.5 versicolor 2 6.9 3.1 4.9 1.5 versicolor 3 5.5 2.3 4.0 1.3 versicolor 4 6.5 2.8 4.6 1.5 versicolor 5 5.7 2.8 4.5 1.3 versicolor 6 6.3 3.3 4.7 1.6 versicolor 7 4.9 2.4 3.3 1.0 versicolor 8 6.6 2.9 4.6 1.3 versicolor 9 5.2 2.7 3.9 1.4 versicolor 10 5.0 2.0 3.5 1.0 versicolor 11 5.9 3.0 4.2 1.5 versicolor 12 6.0 2.2 4.0 1.0 versicolor 13 6.1 2.9 4.7 1.4 versicolor 14 5.6 2.9 3.6 1.3 versicolor 15 6.7 3.1 4.4 1.4 versicolor 16 5.6 3.0 4.5 1.5 versicolor 17 5.8 2.7 4.1 1.0 versicolor 18 6.2 2.2 4.5 1.5 versicolor 19 5.6 2.5 3.9 1.1 versicolor 20 5.9 3.2 4.8 1.8 versicolor 21 6.1 2.8 4.0 1.3 versicolor 22 6.3 2.5 4.9 1.5 versicolor 23 6.1 2.8 4.7 1.2 versicolor 24 6.4 2.9 4.3 1.3 versicolor 25 6.6 3.0 4.4 1.4 versicolor 26 6.8 2.8 4.8 1.4 versicolor 27 6.7 3.0 5.0 1.7 versicolor 28 6.0 2.9 4.5 1.5 versicolor 29 5.7 2.6 3.5 1.0 versicolor 30 5.5 2.4 3.8 1.1 versicolor 31 5.5 2.4 3.7 1.0 versicolor 32 5.8 2.7 3.9 1.2 versicolor 33 6.0 2.7 5.1 1.6 versicolor 34 5.4 3.0 4.5 1.5 versicolor 35 6.0 3.4 4.5 1.6 versicolor 36 6.7 3.1 4.7 1.5 versicolor 37 6.3 2.3 4.4 1.3 versicolor 38 5.6 3.0 4.1 1.3 versicolor 39 5.5 2.5 4.0 1.3 versicolor 40 5.5 2.6 4.4 1.2 versicolor 41 6.1 3.0 4.6 1.4 versicolor 42 5.8 2.6 4.0 1.2 versicolor 43 5.0 2.3 3.3 1.0 versicolor 44 5.6 2.7 4.2 1.3 versicolor 45 5.7 3.0 4.2 1.2 versicolor 46 5.7 2.9 4.2 1.3 versicolor 47 6.2 2.9 4.3 1.3 versicolor 48 5.1 2.5 3.0 1.1 versicolor 49 5.7 2.8 4.1 1.3 versicolor, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 6.3 3.3 6.0 2.5 virginica 1 5.8 2.7 5.1 1.9 virginica 2 7.1 3.0 5.9 2.1 virginica 3 6.3 2.9 5.6 1.8 virginica 4 6.5 3.0 5.8 2.2 virginica 5 7.6 3.0 6.6 2.1 virginica 6 4.9 2.5 4.5 1.7 virginica 7 7.3 2.9 6.3 1.8 virginica 8 6.7 2.5 5.8 1.8 virginica 9 7.2 3.6 6.1 2.5 virginica 10 6.5 3.2 5.1 2.0 virginica 11 6.4 2.7 5.3 1.9 virginica 12 6.8 3.0 5.5 2.1 virginica 13 5.7 2.5 5.0 2.0 virginica 14 5.8 2.8 5.1 2.4 virginica 15 6.4 3.2 5.3 2.3 virginica 16 6.5 3.0 5.5 1.8 virginica 17 7.7 3.8 6.7 2.2 virginica 18 7.7 2.6 6.9 2.3 virginica 19 6.0 2.2 5.0 1.5 virginica 20 6.9 3.2 5.7 2.3 virginica 21 5.6 2.8 4.9 2.0 virginica 22 7.7 2.8 6.7 2.0 virginica 23 6.3 2.7 4.9 1.8 virginica 24 6.7 3.3 5.7 2.1 virginica 25 7.2 3.2 6.0 1.8 virginica 26 6.2 2.8 4.8 1.8 virginica 27 6.1 3.0 4.9 1.8 virginica 28 6.4 2.8 5.6 2.1 virginica 29 7.2 3.0 5.8 1.6 virginica 30 7.4 2.8 6.1 1.9 virginica 31 7.9 3.8 6.4 2.0 virginica 32 6.4 2.8 5.6 2.2 virginica 33 6.3 2.8 5.1 1.5 virginica 34 6.1 2.6 5.6 1.4 virginica 35 7.7 3.0 6.1 2.3 virginica 36 6.3 3.4 5.6 2.4 virginica 37 6.4 3.1 5.5 1.8 virginica 38 6.0 3.0 4.8 1.8 virginica 39 6.9 3.1 5.4 2.1 virginica 40 6.7 3.1 5.6 2.4 virginica 41 6.9 3.1 5.1 2.3 virginica 42 5.8 2.7 5.1 1.9 virginica 43 6.8 3.2 5.9 2.3 virginica 44 6.7 3.3 5.7 2.5 virginica 45 6.7 3.0 5.2 2.3 virginica 46 6.3 2.5 5.0 1.9 virginica 47 6.5 3.0 5.2 2.0 virginica 48 6.2 3.4 5.4 2.3 virginica 49 5.9 3.0 5.1 1.8 virginica]
In [3]:
Copied!
group_keys(ir)
group_keys(ir)
Out[3]:
Species | |
---|---|
<object> | |
0 | setosa |
1 | versicolor |
2 | virginica |
In [4]:
Copied!
ir = iris >> \
group_by(f.Species) >> \
filter(f['Sepal_Length'] > mean(f['Sepal_Length']))
list(group_split(ir))
ir = iris >> \
group_by(f.Species) >> \
filter(f['Sepal_Length'] > mean(f['Sepal_Length']))
list(group_split(ir))
Out[4]:
[ Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 5.1 3.5 1.4 0.2 setosa 1 5.4 3.9 1.7 0.4 setosa 2 5.4 3.7 1.5 0.2 setosa 3 5.8 4.0 1.2 0.2 setosa 4 5.7 4.4 1.5 0.4 setosa 5 5.4 3.9 1.3 0.4 setosa 6 5.1 3.5 1.4 0.3 setosa 7 5.7 3.8 1.7 0.3 setosa 8 5.1 3.8 1.5 0.3 setosa 9 5.4 3.4 1.7 0.2 setosa 10 5.1 3.7 1.5 0.4 setosa 11 5.1 3.3 1.7 0.5 setosa 12 5.2 3.5 1.5 0.2 setosa 13 5.2 3.4 1.4 0.2 setosa 14 5.4 3.4 1.5 0.4 setosa 15 5.2 4.1 1.5 0.1 setosa 16 5.5 4.2 1.4 0.2 setosa 17 5.5 3.5 1.3 0.2 setosa 18 5.1 3.4 1.5 0.2 setosa 19 5.1 3.8 1.9 0.4 setosa 20 5.1 3.8 1.6 0.2 setosa 21 5.3 3.7 1.5 0.2 setosa, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 7.0 3.2 4.7 1.4 versicolor 1 6.4 3.2 4.5 1.5 versicolor 2 6.9 3.1 4.9 1.5 versicolor 3 6.5 2.8 4.6 1.5 versicolor 4 6.3 3.3 4.7 1.6 versicolor 5 6.6 2.9 4.6 1.3 versicolor 6 6.0 2.2 4.0 1.0 versicolor 7 6.1 2.9 4.7 1.4 versicolor 8 6.7 3.1 4.4 1.4 versicolor 9 6.2 2.2 4.5 1.5 versicolor 10 6.1 2.8 4.0 1.3 versicolor 11 6.3 2.5 4.9 1.5 versicolor 12 6.1 2.8 4.7 1.2 versicolor 13 6.4 2.9 4.3 1.3 versicolor 14 6.6 3.0 4.4 1.4 versicolor 15 6.8 2.8 4.8 1.4 versicolor 16 6.7 3.0 5.0 1.7 versicolor 17 6.0 2.9 4.5 1.5 versicolor 18 6.0 2.7 5.1 1.6 versicolor 19 6.0 3.4 4.5 1.6 versicolor 20 6.7 3.1 4.7 1.5 versicolor 21 6.3 2.3 4.4 1.3 versicolor 22 6.1 3.0 4.6 1.4 versicolor 23 6.2 2.9 4.3 1.3 versicolor, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 7.1 3.0 5.9 2.1 virginica 1 7.6 3.0 6.6 2.1 virginica 2 7.3 2.9 6.3 1.8 virginica 3 6.7 2.5 5.8 1.8 virginica 4 7.2 3.6 6.1 2.5 virginica 5 6.8 3.0 5.5 2.1 virginica 6 7.7 3.8 6.7 2.2 virginica 7 7.7 2.6 6.9 2.3 virginica 8 6.9 3.2 5.7 2.3 virginica 9 7.7 2.8 6.7 2.0 virginica 10 6.7 3.3 5.7 2.1 virginica 11 7.2 3.2 6.0 1.8 virginica 12 7.2 3.0 5.8 1.6 virginica 13 7.4 2.8 6.1 1.9 virginica 14 7.9 3.8 6.4 2.0 virginica 15 7.7 3.0 6.1 2.3 virginica 16 6.9 3.1 5.4 2.1 virginica 17 6.7 3.1 5.6 2.4 virginica 18 6.9 3.1 5.1 2.3 virginica 19 6.8 3.2 5.9 2.3 virginica 20 6.7 3.3 5.7 2.5 virginica 21 6.7 3.0 5.2 2.3 virginica]
In [5]:
Copied!
group_keys(ir)
group_keys(ir)
Out[5]:
Species | |
---|---|
<object> | |
0 | setosa |
1 | versicolor |
2 | virginica |
In [6]:
Copied!
list(iris >> group_split(f.Species))
list(iris >> group_split(f.Species))
Out[6]:
[ Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa 5 5.4 3.9 1.7 0.4 setosa 6 4.6 3.4 1.4 0.3 setosa 7 5.0 3.4 1.5 0.2 setosa 8 4.4 2.9 1.4 0.2 setosa 9 4.9 3.1 1.5 0.1 setosa 10 5.4 3.7 1.5 0.2 setosa 11 4.8 3.4 1.6 0.2 setosa 12 4.8 3.0 1.4 0.1 setosa 13 4.3 3.0 1.1 0.1 setosa 14 5.8 4.0 1.2 0.2 setosa 15 5.7 4.4 1.5 0.4 setosa 16 5.4 3.9 1.3 0.4 setosa 17 5.1 3.5 1.4 0.3 setosa 18 5.7 3.8 1.7 0.3 setosa 19 5.1 3.8 1.5 0.3 setosa 20 5.4 3.4 1.7 0.2 setosa 21 5.1 3.7 1.5 0.4 setosa 22 4.6 3.6 1.0 0.2 setosa 23 5.1 3.3 1.7 0.5 setosa 24 4.8 3.4 1.9 0.2 setosa 25 5.0 3.0 1.6 0.2 setosa 26 5.0 3.4 1.6 0.4 setosa 27 5.2 3.5 1.5 0.2 setosa 28 5.2 3.4 1.4 0.2 setosa 29 4.7 3.2 1.6 0.2 setosa 30 4.8 3.1 1.6 0.2 setosa 31 5.4 3.4 1.5 0.4 setosa 32 5.2 4.1 1.5 0.1 setosa 33 5.5 4.2 1.4 0.2 setosa 34 4.9 3.1 1.5 0.2 setosa 35 5.0 3.2 1.2 0.2 setosa 36 5.5 3.5 1.3 0.2 setosa 37 4.9 3.6 1.4 0.1 setosa 38 4.4 3.0 1.3 0.2 setosa 39 5.1 3.4 1.5 0.2 setosa 40 5.0 3.5 1.3 0.3 setosa 41 4.5 2.3 1.3 0.3 setosa 42 4.4 3.2 1.3 0.2 setosa 43 5.0 3.5 1.6 0.6 setosa 44 5.1 3.8 1.9 0.4 setosa 45 4.8 3.0 1.4 0.3 setosa 46 5.1 3.8 1.6 0.2 setosa 47 4.6 3.2 1.4 0.2 setosa 48 5.3 3.7 1.5 0.2 setosa 49 5.0 3.3 1.4 0.2 setosa, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 7.0 3.2 4.7 1.4 versicolor 1 6.4 3.2 4.5 1.5 versicolor 2 6.9 3.1 4.9 1.5 versicolor 3 5.5 2.3 4.0 1.3 versicolor 4 6.5 2.8 4.6 1.5 versicolor 5 5.7 2.8 4.5 1.3 versicolor 6 6.3 3.3 4.7 1.6 versicolor 7 4.9 2.4 3.3 1.0 versicolor 8 6.6 2.9 4.6 1.3 versicolor 9 5.2 2.7 3.9 1.4 versicolor 10 5.0 2.0 3.5 1.0 versicolor 11 5.9 3.0 4.2 1.5 versicolor 12 6.0 2.2 4.0 1.0 versicolor 13 6.1 2.9 4.7 1.4 versicolor 14 5.6 2.9 3.6 1.3 versicolor 15 6.7 3.1 4.4 1.4 versicolor 16 5.6 3.0 4.5 1.5 versicolor 17 5.8 2.7 4.1 1.0 versicolor 18 6.2 2.2 4.5 1.5 versicolor 19 5.6 2.5 3.9 1.1 versicolor 20 5.9 3.2 4.8 1.8 versicolor 21 6.1 2.8 4.0 1.3 versicolor 22 6.3 2.5 4.9 1.5 versicolor 23 6.1 2.8 4.7 1.2 versicolor 24 6.4 2.9 4.3 1.3 versicolor 25 6.6 3.0 4.4 1.4 versicolor 26 6.8 2.8 4.8 1.4 versicolor 27 6.7 3.0 5.0 1.7 versicolor 28 6.0 2.9 4.5 1.5 versicolor 29 5.7 2.6 3.5 1.0 versicolor 30 5.5 2.4 3.8 1.1 versicolor 31 5.5 2.4 3.7 1.0 versicolor 32 5.8 2.7 3.9 1.2 versicolor 33 6.0 2.7 5.1 1.6 versicolor 34 5.4 3.0 4.5 1.5 versicolor 35 6.0 3.4 4.5 1.6 versicolor 36 6.7 3.1 4.7 1.5 versicolor 37 6.3 2.3 4.4 1.3 versicolor 38 5.6 3.0 4.1 1.3 versicolor 39 5.5 2.5 4.0 1.3 versicolor 40 5.5 2.6 4.4 1.2 versicolor 41 6.1 3.0 4.6 1.4 versicolor 42 5.8 2.6 4.0 1.2 versicolor 43 5.0 2.3 3.3 1.0 versicolor 44 5.6 2.7 4.2 1.3 versicolor 45 5.7 3.0 4.2 1.2 versicolor 46 5.7 2.9 4.2 1.3 versicolor 47 6.2 2.9 4.3 1.3 versicolor 48 5.1 2.5 3.0 1.1 versicolor 49 5.7 2.8 4.1 1.3 versicolor, Sepal_Length Sepal_Width Petal_Length Petal_Width Species <float64> <float64> <float64> <float64> <object> 0 6.3 3.3 6.0 2.5 virginica 1 5.8 2.7 5.1 1.9 virginica 2 7.1 3.0 5.9 2.1 virginica 3 6.3 2.9 5.6 1.8 virginica 4 6.5 3.0 5.8 2.2 virginica 5 7.6 3.0 6.6 2.1 virginica 6 4.9 2.5 4.5 1.7 virginica 7 7.3 2.9 6.3 1.8 virginica 8 6.7 2.5 5.8 1.8 virginica 9 7.2 3.6 6.1 2.5 virginica 10 6.5 3.2 5.1 2.0 virginica 11 6.4 2.7 5.3 1.9 virginica 12 6.8 3.0 5.5 2.1 virginica 13 5.7 2.5 5.0 2.0 virginica 14 5.8 2.8 5.1 2.4 virginica 15 6.4 3.2 5.3 2.3 virginica 16 6.5 3.0 5.5 1.8 virginica 17 7.7 3.8 6.7 2.2 virginica 18 7.7 2.6 6.9 2.3 virginica 19 6.0 2.2 5.0 1.5 virginica 20 6.9 3.2 5.7 2.3 virginica 21 5.6 2.8 4.9 2.0 virginica 22 7.7 2.8 6.7 2.0 virginica 23 6.3 2.7 4.9 1.8 virginica 24 6.7 3.3 5.7 2.1 virginica 25 7.2 3.2 6.0 1.8 virginica 26 6.2 2.8 4.8 1.8 virginica 27 6.1 3.0 4.9 1.8 virginica 28 6.4 2.8 5.6 2.1 virginica 29 7.2 3.0 5.8 1.6 virginica 30 7.4 2.8 6.1 1.9 virginica 31 7.9 3.8 6.4 2.0 virginica 32 6.4 2.8 5.6 2.2 virginica 33 6.3 2.8 5.1 1.5 virginica 34 6.1 2.6 5.6 1.4 virginica 35 7.7 3.0 6.1 2.3 virginica 36 6.3 3.4 5.6 2.4 virginica 37 6.4 3.1 5.5 1.8 virginica 38 6.0 3.0 4.8 1.8 virginica 39 6.9 3.1 5.4 2.1 virginica 40 6.7 3.1 5.6 2.4 virginica 41 6.9 3.1 5.1 2.3 virginica 42 5.8 2.7 5.1 1.9 virginica 43 6.8 3.2 5.9 2.3 virginica 44 6.7 3.3 5.7 2.5 virginica 45 6.7 3.0 5.2 2.3 virginica 46 6.3 2.5 5.0 1.9 virginica 47 6.5 3.0 5.2 2.0 virginica 48 6.2 3.4 5.4 2.3 virginica 49 5.9 3.0 5.1 1.8 virginica]
In [7]:
Copied!
# group_keys(...) deprecated in dplyr
iris >> group_by(f.Species) >> group_keys()
# group_keys(...) deprecated in dplyr
iris >> group_by(f.Species) >> group_keys()
Out[7]:
Species | |
---|---|
<object> | |
0 | setosa |
1 | versicolor |
2 | virginica |
In [8]:
Copied!
out = iris >> group_by(f.Species) >> group_keys() >> rowwise() >> group_split()
out
out = iris >> group_by(f.Species) >> group_keys() >> rowwise() >> group_split()
out
[2022-12-02 14:07:59][datar][WARNING] `_keep` is ignored in `group_split(<TibbleRowwise>)`.
Out[8]:
<generator object group_split_impl at 0x7f4457822190>
In [9]:
Copied!
list(out)
list(out)
Out[9]:
[ Species <object> 0 setosa, Species <object> 0 versicolor, Species <object> 0 virginica]
In [ ]:
Copied!