Last active
July 2, 2023 03:09
-
-
Save dalejung/a31c6d9de0bc0ff6bd19 to your computer and use it in GitHub Desktop.
Untitled1.ipynb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"from pandas.core.groupby import DataFrameGroupBy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def group_counts(grouped):\n", | |
" grouper = grouped.grouper\n", | |
" indices = grouper.indices\n", | |
" \n", | |
" group_keys = grouper._get_group_keys()\n", | |
" counts = [len(indices[k]) for k in group_keys]\n", | |
" return grouped._wrap_applied_output(group_keys, counts)\n", | |
"DataFrameGroupBy.group_counts = group_counts" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.util.testing.makeMixedDataFrame()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"grouped = df.groupby('B')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"B\n", | |
"0 3\n", | |
"1 2\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"grouped.group_counts()" | |
] | |
} | |
], | |
"metadata": { | |
"gist_id": "a31c6d9de0bc0ff6bd19", | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment