Last active
August 7, 2018 20:20
-
-
Save vinaykudari/e264fe2e4b823035d62fbe97ff02163a to your computer and use it in GitHub Desktop.
Group By
This file contains hidden or 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": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sales = {'weekday' : ['Sun', 'Mon', 'Mon', 'Sun', 'Tue', 'Sun', 'Tue'],\n", | |
" 'bread' : [120, 234, 100, 260, 421, 223, 322],\n", | |
" 'butter' : [20, 24, 90, 60, 53, 89, 76]\n", | |
"}\n", | |
"df = pd.DataFrame(sales)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>weekday</th>\n", | |
" <th>bread</th>\n", | |
" <th>butter</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Sun</td>\n", | |
" <td>120</td>\n", | |
" <td>20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>Mon</td>\n", | |
" <td>234</td>\n", | |
" <td>24</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>Mon</td>\n", | |
" <td>100</td>\n", | |
" <td>90</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>Sun</td>\n", | |
" <td>260</td>\n", | |
" <td>60</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>Tue</td>\n", | |
" <td>421</td>\n", | |
" <td>53</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>Sun</td>\n", | |
" <td>223</td>\n", | |
" <td>89</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>Tue</td>\n", | |
" <td>322</td>\n", | |
" <td>76</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" weekday bread butter\n", | |
"0 Sun 120 20\n", | |
"1 Mon 234 24\n", | |
"2 Mon 100 90\n", | |
"3 Sun 260 60\n", | |
"4 Tue 421 53\n", | |
"5 Sun 223 89\n", | |
"6 Tue 322 76" | |
] | |
}, | |
"execution_count": 83, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Mon': Int64Index([1, 2], dtype='int64'),\n", | |
" 'Sun': Int64Index([0, 3, 5], dtype='int64'),\n", | |
" 'Tue': Int64Index([4, 6], dtype='int64')}" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df_grouped = df.groupby(['weekday']) #Grouping by weekday\n", | |
"df_grouped.groups" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Not a Sunday': Int64Index([1, 2, 4, 6], dtype='int64'),\n", | |
" 'Sunday': Int64Index([0, 3, 5], dtype='int64')}" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"condition = (df['weekday'] == 'Sun').map({True:'Sunday', False:'Not a Sunday'}) #Grouping by a condition\n", | |
"df.groupby(condition).groups" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Mon\n", | |
" weekday bread butter\n", | |
"1 Mon 234 24\n", | |
"2 Mon 100 90\n", | |
"\n", | |
"\n", | |
"Sun\n", | |
" weekday bread butter\n", | |
"0 Sun 120 20\n", | |
"3 Sun 260 60\n", | |
"5 Sun 223 89\n", | |
"\n", | |
"\n", | |
"Tue\n", | |
" weekday bread butter\n", | |
"4 Tue 421 53\n", | |
"6 Tue 322 76\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"for name,group in df_grouped: #Interating through the groups\n", | |
" print(name)\n", | |
" print(group)\n", | |
" print('\\n')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment