bind
# https://dplyr.tidyverse.org/reference/bind.html
%run nb_helpers.py
from datar.data import starwars
from datar.all import *
nb_header(bind_rows, bind_cols, book='bind')
★ bind_rows¶
Bind rows of give dataframes¶
Original APIs https://dplyr.tidyverse.org/reference/bind.html
Args:¶
*data
: Dataframes to combine
_id
: The name of the id columns
_copy
: If False
, do not copy data unnecessarily.
Original API does not support this. This argument will be
passed by to pandas.concat()
as copy
argument.
**kwargs
: A mapping of dataframe, keys will be used as _id col.
Returns:¶
The combined dataframe
★ bind_cols¶
Bind columns of give dataframes¶
Note that unlike dplyr
, mismatched dimensions are allowed and
missing rows will be filled with NA
s
Args:¶
*data
: Dataframes to bind
_name_repair
: treatment of problematic column names:
- "minimal": No name repair or checks, beyond basic existence,
- "unique": Make sure names are unique and not empty,
- "check_unique": (default value), no name repair, but check they are unique,
- "universal": Make the names unique and syntactic
- a function: apply custom name repair
_copy
: If False
, do not copy data unnecessarily.
Original API does not support this. This argument will be
passed by to pandas.concat()
as copy
argument.
Returns:¶
The combined dataframe
one = starwars >> slice(c[:4])
two = starwars >> slice(c[9:12])
one >> bind_rows(two)
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
bind_rows([one, two])
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
bind_rows([one, two], [two, one])
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
7 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
8 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
9 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
10 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
11 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
12 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
13 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
tibble(a=1, b=2) >> bind_rows(tibble(a=3, b=4))
a | b | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 2 |
1 | 3 | 4 |
tibble(a=1, b=2) >> bind_rows(
tibble(a=[3, 4], b=[5, 6]),
tibble(a=7, b=8)
)
a | b | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 2 |
1 | 3 | 5 |
2 | 4 | 6 |
3 | 7 | 8 |
bind_rows([one, two], _id = "id")
id | name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
<int64> | <object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | 0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | 0 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | 0 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | 0 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | 1 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | 1 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | 1 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
# If we need a name for one
bind_rows(a=one, b=two, _id = "id")
id | name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | a | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | a | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | a | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | a | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | b | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | b | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | b | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
bind_rows(**{"group 1": one, "group 2": two}, _id = "groups")
groups | name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | group 1 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
1 | group 1 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | Droid |
2 | group 1 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
3 | group 1 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | group 2 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
5 | group 2 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
6 | group 2 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
tibble(x=range(3)) >> bind_rows(
tibble(y=range(4))
)
x | y | |
---|---|---|
<float64> | <float64> | |
0 | 0.0 | NaN |
1 | 1.0 | NaN |
2 | 2.0 | NaN |
3 | NaN | 0.0 |
4 | NaN | 1.0 |
5 | NaN | 2.0 |
6 | NaN | 3.0 |
# NAs filled for missed rows
tibble(x=range(3)) >> bind_cols(
tibble(y=range(2))
)
x | y | |
---|---|---|
<int64> | <float64> | |
0 | 0 | 0.0 |
1 | 1 | 1.0 |
2 | 2 | NaN |
tibble(x = range(3)) >> bind_cols(tibble())
x | |
---|---|
<int64> | |
0 | 0 |
1 | 1 |
2 | 2 |
one >> bind_cols(two)
[2022-12-02 13:42:54][datar][WARNING] New names: [2022-12-02 13:42:54][datar][WARNING] * 'name' -> 'name__0' [2022-12-02 13:42:54][datar][WARNING] * 'height' -> 'height__1' [2022-12-02 13:42:54][datar][WARNING] * 'mass' -> 'mass__2' [2022-12-02 13:42:54][datar][WARNING] * 'hair_color' -> 'hair_color__3' [2022-12-02 13:42:54][datar][WARNING] * 'skin_color' -> 'skin_color__4' [2022-12-02 13:42:54][datar][WARNING] * 'eye_color' -> 'eye_color__5' [2022-12-02 13:42:54][datar][WARNING] * 'birth_year' -> 'birth_year__6' [2022-12-02 13:42:54][datar][WARNING] * 'sex' -> 'sex__7' [2022-12-02 13:42:54][datar][WARNING] * 'gender' -> 'gender__8' [2022-12-02 13:42:54][datar][WARNING] * 'homeworld' -> 'homeworld__9' [2022-12-02 13:42:54][datar][WARNING] * 'species' -> 'species__10' [2022-12-02 13:42:54][datar][WARNING] * 'name' -> 'name__11' [2022-12-02 13:42:54][datar][WARNING] * 'height' -> 'height__12' [2022-12-02 13:42:54][datar][WARNING] * 'mass' -> 'mass__13' [2022-12-02 13:42:54][datar][WARNING] * 'hair_color' -> 'hair_color__14' [2022-12-02 13:42:54][datar][WARNING] * 'skin_color' -> 'skin_color__15' [2022-12-02 13:42:54][datar][WARNING] * 'eye_color' -> 'eye_color__16' [2022-12-02 13:42:54][datar][WARNING] * 'birth_year' -> 'birth_year__17' [2022-12-02 13:42:54][datar][WARNING] * 'sex' -> 'sex__18' [2022-12-02 13:42:54][datar][WARNING] * 'gender' -> 'gender__19' [2022-12-02 13:42:54][datar][WARNING] * 'homeworld' -> 'homeworld__20' [2022-12-02 13:42:54][datar][WARNING] * 'species' -> 'species__21'
name__0 | height__1 | mass__2 | hair_color__3 | skin_color__4 | eye_color__5 | birth_year__6 | sex__7 | gender__8 | homeworld__9 | ... | height__12 | mass__13 | hair_color__14 | skin_color__15 | eye_color__16 | birth_year__17 | sex__18 | gender__19 | homeworld__20 | species__21 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | ... | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | C-3PO | 167.0 | 75.0 | NaN | gold | yellow | 112.0 | none | masculine | Tatooine | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
9 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
10 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
11 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
7 rows × 22 columns