fill
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/fill.html
%run nb_helpers.py
from datar.all import *
nb_header(fill)
# https://tidyr.tidyverse.org/reference/fill.html
%run nb_helpers.py
from datar.all import *
nb_header(fill)
Try this notebook on binder.
★ fill¶
Fills missing values in selected columns using the next or¶
previous entry.
See https://tidyr.tidyverse.org/reference/fill.html
Args:¶
_data
: A dataframe
*columns
: Columns to fill
_direction
: Direction in which to fill missing values.
Currently either "down" (the default), "up",
"downup" (i.e. first down and then up) or
"updown" (first up and then down).
Returns:¶
The dataframe with NAs being replaced.
In [2]:
Copied!
sales = tribble(
f.quarter, f.year, f.sales,
"Q1", 2000, 66013,
"Q2", NA, 69182,
"Q3", NA, 53175,
"Q4", NA, 21001,
"Q1", 2001, 46036,
"Q2", NA, 58842,
"Q3", NA, 44568,
"Q4", NA, 50197,
"Q1", 2002, 39113,
"Q2", NA, 41668,
"Q3", NA, 30144,
"Q4", NA, 52897,
"Q1", 2004, 32129,
"Q2", NA, 67686,
"Q3", NA, 31768,
"Q4", NA, 49094
)
sales
sales = tribble(
f.quarter, f.year, f.sales,
"Q1", 2000, 66013,
"Q2", NA, 69182,
"Q3", NA, 53175,
"Q4", NA, 21001,
"Q1", 2001, 46036,
"Q2", NA, 58842,
"Q3", NA, 44568,
"Q4", NA, 50197,
"Q1", 2002, 39113,
"Q2", NA, 41668,
"Q3", NA, 30144,
"Q4", NA, 52897,
"Q1", 2004, 32129,
"Q2", NA, 67686,
"Q3", NA, 31768,
"Q4", NA, 49094
)
sales
Out[2]:
quarter | year | sales | |
---|---|---|---|
<object> | <float64> | <int64> | |
0 | Q1 | 2000.0 | 66013 |
1 | Q2 | NaN | 69182 |
2 | Q3 | NaN | 53175 |
3 | Q4 | NaN | 21001 |
4 | Q1 | 2001.0 | 46036 |
5 | Q2 | NaN | 58842 |
6 | Q3 | NaN | 44568 |
7 | Q4 | NaN | 50197 |
8 | Q1 | 2002.0 | 39113 |
9 | Q2 | NaN | 41668 |
10 | Q3 | NaN | 30144 |
11 | Q4 | NaN | 52897 |
12 | Q1 | 2004.0 | 32129 |
13 | Q2 | NaN | 67686 |
14 | Q3 | NaN | 31768 |
15 | Q4 | NaN | 49094 |
In [3]:
Copied!
sales >> fill(f.year)
sales >> fill(f.year)
Out[3]:
quarter | year | sales | |
---|---|---|---|
<object> | <float64> | <int64> | |
0 | Q1 | 2000.0 | 66013 |
1 | Q2 | 2000.0 | 69182 |
2 | Q3 | 2000.0 | 53175 |
3 | Q4 | 2000.0 | 21001 |
4 | Q1 | 2001.0 | 46036 |
5 | Q2 | 2001.0 | 58842 |
6 | Q3 | 2001.0 | 44568 |
7 | Q4 | 2001.0 | 50197 |
8 | Q1 | 2002.0 | 39113 |
9 | Q2 | 2002.0 | 41668 |
10 | Q3 | 2002.0 | 30144 |
11 | Q4 | 2002.0 | 52897 |
12 | Q1 | 2004.0 | 32129 |
13 | Q2 | 2004.0 | 67686 |
14 | Q3 | 2004.0 | 31768 |
15 | Q4 | 2004.0 | 49094 |
In [4]:
Copied!
tidy_pets = tribble(
f.rank, f.pet_type, f.breed,
1, NA, "Boston Terrier",
2, NA, "Retrievers (Labrador)",
3, NA, "Retrievers (Golden)",
4, NA, "French Bulldogs",
5, NA, "Bulldogs",
6, "Dog", "Beagles",
1, NA, "Persian",
2, NA, "Maine Coon",
3, NA, "Ragdoll",
4, NA, "Exotic",
5, NA, "Siamese",
6, "Cat", "American Short"
)
tidy_pets >> fill(f.pet_type, _direction = "up")
tidy_pets = tribble(
f.rank, f.pet_type, f.breed,
1, NA, "Boston Terrier",
2, NA, "Retrievers (Labrador)",
3, NA, "Retrievers (Golden)",
4, NA, "French Bulldogs",
5, NA, "Bulldogs",
6, "Dog", "Beagles",
1, NA, "Persian",
2, NA, "Maine Coon",
3, NA, "Ragdoll",
4, NA, "Exotic",
5, NA, "Siamese",
6, "Cat", "American Short"
)
tidy_pets >> fill(f.pet_type, _direction = "up")
Out[4]:
rank | pet_type | breed | |
---|---|---|---|
<int64> | <object> | <object> | |
0 | 1 | Dog | Boston Terrier |
1 | 2 | Dog | Retrievers (Labrador) |
2 | 3 | Dog | Retrievers (Golden) |
3 | 4 | Dog | French Bulldogs |
4 | 5 | Dog | Bulldogs |
5 | 6 | Dog | Beagles |
6 | 1 | Cat | Persian |
7 | 2 | Cat | Maine Coon |
8 | 3 | Cat | Ragdoll |
9 | 4 | Cat | Exotic |
10 | 5 | Cat | Siamese |
11 | 6 | Cat | American Short |
In [5]:
Copied!
squirrels = tribble(
f.group, f.name, f.role, f.n_squirrels,
1, "Sam", "Observer", NA,
1, "Mara", "Scorekeeper", 8,
1, "Jesse", "Observer", NA,
1, "Tom", "Observer", NA,
2, "Mike", "Observer", NA,
2, "Rachael", "Observer", NA,
2, "Sydekea", "Scorekeeper", 14,
2, "Gabriela", "Observer", NA,
3, "Derrick", "Observer", NA,
3, "Kara", "Scorekeeper", 9,
3, "Emily", "Observer", NA,
3, "Danielle", "Observer", NA
)
squirrels >> \
group_by(f.group) >> \
fill(f.n_squirrels, _direction = "downup")
squirrels = tribble(
f.group, f.name, f.role, f.n_squirrels,
1, "Sam", "Observer", NA,
1, "Mara", "Scorekeeper", 8,
1, "Jesse", "Observer", NA,
1, "Tom", "Observer", NA,
2, "Mike", "Observer", NA,
2, "Rachael", "Observer", NA,
2, "Sydekea", "Scorekeeper", 14,
2, "Gabriela", "Observer", NA,
3, "Derrick", "Observer", NA,
3, "Kara", "Scorekeeper", 9,
3, "Emily", "Observer", NA,
3, "Danielle", "Observer", NA
)
squirrels >> \
group_by(f.group) >> \
fill(f.n_squirrels, _direction = "downup")
Out[5]:
group | name | role | n_squirrels | |
---|---|---|---|---|
<int64> | <object> | <object> | <float64> | |
0 | 1 | Sam | Observer | 8.0 |
1 | 1 | Mara | Scorekeeper | 8.0 |
2 | 1 | Jesse | Observer | 8.0 |
3 | 1 | Tom | Observer | 8.0 |
4 | 2 | Mike | Observer | 14.0 |
5 | 2 | Rachael | Observer | 14.0 |
6 | 2 | Sydekea | Scorekeeper | 14.0 |
7 | 2 | Gabriela | Observer | 14.0 |
8 | 3 | Derrick | Observer | 9.0 |
9 | 3 | Kara | Scorekeeper | 9.0 |
10 | 3 | Emily | Observer | 9.0 |
11 | 3 | Danielle | Observer | 9.0 |
TibbleGrouped: group (n=3)
In [ ]:
Copied!