Last active
August 11, 2020 16:18
-
-
Save BenjaminWolfe/86508a6ec184493795bd8e6ecb3e007d to your computer and use it in GitHub Desktop.
grouping, ordering, and filling in Python and R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, pandas as pd | |
# code to be compared to group-and-fill.R | |
# task: fill specific columns down, within each group, ordering by the order. | |
df = pd.DataFrame( | |
columns=["group", "order", "attribute_1", "attribute_2", "irrelevant"], | |
data = [ | |
["a", 0, 1, 1, "hello"], | |
["a", 2, np.nan, 3, np.nan], # this one out of order | |
["a", 1, 6, np.nan, "world"], | |
["b", 0, 2, 7, "foo" ], | |
["b", 1, np.nan, 10, "bar" ], | |
] | |
) | |
df.sort_values(by=["group", "order"], inplace=True) | |
df[["attribute_1", "attribute_2"]] = ( | |
df | |
.groupby("group")["attribute_1", "attribute_2"] | |
.apply(lambda group: group.ffill()) | |
) | |
df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code to be compared to group-and-fill.py | |
# task: fill specific columns down, within each group, ordering by the order. | |
library(tidyverse) | |
df <- tribble( | |
~group, ~order, ~attribute_1, ~attribute_2, ~irrelevant, | |
"a", 0, 1, 1, "hello", | |
"a", 2, NA, 3, NA , # this one out of ordr | |
"a", 1, 6, NA, "world", | |
"b", 0, 2, 7, "foo" , | |
"b", 1, NA, 10, "bar" | |
) | |
df %>% | |
group_by(group) %>% | |
arrange(group, order) %>% | |
fill(attribute_1, attribute_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment