Skip to content

Instantly share code, notes, and snippets.

@scottpham
Created April 20, 2021 22:41
Show Gist options
  • Save scottpham/596322e3300a3f1fca26dc05f75943a6 to your computer and use it in GitHub Desktop.
Save scottpham/596322e3300a3f1fca26dc05f75943a6 to your computer and use it in GitHub Desktop.
How to use apply with groupby in pandas
# grooop
grouped = with_region.groupby('level_comp_region')
# How many workers are in which
res = (
grouped
.apply( lambda grp: pd.Series({
"old_min": grp["old_min"].iloc[0],
"old_max": grp["old_max"].iloc[0],
"employees here": len(grp)
}))
.sort_values(['employees here'])
.reset_index()
)
res.head(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment