Created
April 20, 2021 22:41
-
-
Save scottpham/596322e3300a3f1fca26dc05f75943a6 to your computer and use it in GitHub Desktop.
How to use apply with groupby in pandas
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
# 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