replace_na
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/replace_na.html
%run nb_helpers.py
from datar.all import *
nb_header(replace_na)
# https://tidyr.tidyverse.org/reference/replace_na.html
%run nb_helpers.py
from datar.all import *
nb_header(replace_na)
Try this notebook on binder.
★ replace_na¶
Replace NA with a value¶
This function can be also used not as a verb. As a function called as
an argument in a verb, data is passed implicitly. Then one could
pass data_or_replace as the data to replace.
Args:¶
data
: The data piped in
data_or_replace
: When called as argument of a verb, this is the
data to replace. Otherwise this is the replacement.
replace
: The value to replace with
Can only be a scalar or dict for data frame.
So replace NA with a list is not supported yet.
Returns:¶
Corresponding data with NAs replaced
In [2]:
Copied!
df = tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
df >> replace_na(dict(x = 0, y = "unknown"))
df = tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
df >> replace_na(dict(x = 0, y = "unknown"))
Out[2]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
1 | 2.0 | unknown |
2 | 0.0 | b |
In [3]:
Copied!
df >> mutate(x = replace_na(f.x, 0))
df >> mutate(x = replace_na(f.x, 0))
Out[3]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
1 | 2.0 | NaN |
2 | 0.0 | b |
In [4]:
Copied!
df.x >> replace_na(0)
df.x >> replace_na(0)
Out[4]:
0 1.0 1 2.0 2 0.0 Name: x, dtype: float64
In [5]:
Copied!
df.y >> replace_na("unknown")
df.y >> replace_na("unknown")
Out[5]:
0 a 1 unknown 2 b Name: y, dtype: object
In [6]:
Copied!
df_list = tibble(z = [seq(1,5), NULL, seq(10,20)])
df_list >> replace_na({'z': 5}) # replace with a list not supported yet
df_list = tibble(z = [seq(1,5), NULL, seq(10,20)])
df_list >> replace_na({'z': 5}) # replace with a list not supported yet
Out[6]:
z | |
---|---|
<object> | |
0 | [1, 2, 3, 4, 5] |
1 | 5 |
2 | [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |