Skip to content

Instantly share code, notes, and snippets.

@BenjaminWolfe
Last active August 11, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenjaminWolfe/86508a6ec184493795bd8e6ecb3e007d to your computer and use it in GitHub Desktop.
Save BenjaminWolfe/86508a6ec184493795bd8e6ecb3e007d to your computer and use it in GitHub Desktop.
grouping, ordering, and filling in Python and R
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
# 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