unite
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/unite.html
%run nb_helpers.py
from datar.all import *
nb_header(unite)
# https://tidyr.tidyverse.org/reference/unite.html
%run nb_helpers.py
from datar.all import *
nb_header(unite)
Try this notebook on binder.
★ unite¶
Unite multiple columns into one by pasting strings together¶
Args:¶
data: A data frame.
col: The name of the new column, as a string or symbol.
*columns: Columns to unite
sep: Separator to use between values.
remove: If True, remove input columns from output data frame.
na_rm: If True, missing values will be remove prior to uniting
each value.
Returns:¶
The dataframe with selected columns united
In [2]:
Copied!
df = expand_grid(x=c("a", NA), y=c("b", NA))
df
df = expand_grid(x=c("a", NA), y=c("b", NA))
df
Out[2]:
| x | y | |
|---|---|---|
| <object> | <object> | |
| 0 | a | b |
| 1 | a | NaN |
| 2 | NaN | b |
| 3 | NaN | NaN |
In [3]:
Copied!
df >> unite("z", c(f.x, f.y), remove=False)
df >> unite("z", c(f.x, f.y), remove=False)
Out[3]:
| z | x | y | |
|---|---|---|---|
| <object> | <object> | <object> | |
| 0 | a_b | a | b |
| 1 | a | a | NaN |
| 2 | b | NaN | b |
| 3 | NaN | NaN |
In [4]:
Copied!
df >> unite("z", [0, 1], na_rm=True, remove=False)
df >> unite("z", [0, 1], na_rm=True, remove=False)
Out[4]:
| z | x | y | |
|---|---|---|---|
| <object> | <object> | <object> | |
| 0 | a_b | a | b |
| 1 | a | a | NaN |
| 2 | b | NaN | b |
| 3 | NaN | NaN |
In [5]:
Copied!
df >> \
unite("xy", c(f.x, f.y)) >> \
separate(f.xy, c("x", "y"))
df >> \
unite("xy", c(f.x, f.y)) >> \
separate(f.xy, c("x", "y"))
[2022-12-02 14:51:25][datar][WARNING] Expected 2 pieces. Missing pieces filled with `NA` in 3 rows ['a', 'b', ''].
Out[5]:
| x | y | |
|---|---|---|
| <object> | <object> | |
| 0 | a | b |
| 1 | a | NaN |
| 2 | b | NaN |
| 3 | NaN |