drop_na
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/drop_na.html
%run nb_helpers.py
from datar.all import *
nb_header(drop_na)
# https://tidyr.tidyverse.org/reference/drop_na.html
%run nb_helpers.py
from datar.all import *
nb_header(drop_na)
Try this notebook on binder.
★ drop_na¶
Drop rows containing missing values¶
See https://tidyr.tidyverse.org/reference/drop_na.html
Args:¶
data
: A data frame.
*columns
: Columns to inspect for missing values.
_how
: How to select the rows to drop
- all: All columns of columns
to be NA
s
- any: Any columns of columns
to be NA
s
(tidyr doesn't support this argument)
Returns:¶
Dataframe with rows with NAs dropped and indexes dropped
In [2]:
Copied!
df = tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
df >> drop_na()
df = tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
df >> drop_na()
Out[2]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
In [3]:
Copied!
df >> drop_na(f.x)
df >> drop_na(f.x)
Out[3]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
1 | 2.0 | NaN |
In [4]:
Copied!
vars = ["y"]
df >> drop_na(f.x, any_of(vars))
vars = ["y"]
df >> drop_na(f.x, any_of(vars))
Out[4]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
In [5]:
Copied!
# how_='any' or how_='all'
# not supported by tidyr
df >> drop_na(how_='all')
# how_='any' or how_='all'
# not supported by tidyr
df >> drop_na(how_='all')
Out[5]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |
1 | 2.0 | NaN |
2 | NaN | b |
In [6]:
Copied!
df >> drop_na(how_='any')
df >> drop_na(how_='any')
Out[6]:
x | y | |
---|---|---|
<float64> | <object> | |
0 | 1.0 | a |