Last active
October 10, 2021 11:15
-
-
Save Caellwyn/0866534f6d94ce6dd0137f90d9f690bf to your computer and use it in GitHub Desktop.
Pandas df.groupby() Example
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "pandas_groupby_example.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyO64/h+qKt7uO/BsVmgroV1", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Caellwyn/0866534f6d94ce6dd0137f90d9f690bf/untitled5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-BPcWDDH_-c1" | |
}, | |
"source": [ | |
"import pandas as pd\r\n", | |
"import zipfile\r\n", | |
"import wget\r\n", | |
"\r\n", | |
"url = 'https://analyse.kmi.open.ac.uk/open_dataset/download'\r\n", | |
"# filename = wget.download(url)\r\n", | |
"zpf = zipfile.ZipFile(filename)\r\n", | |
"student_vle = pd.read_csv(zpf.open('studentVle.csv'))\r\n", | |
"\r\n", | |
"index_cols = ['code_module','code_presentation','id_student']\r\n", | |
"student_groups = student_vle.groupby(index_cols)\r\n", | |
"\r\n", | |
"activity_counts = student_groups.count()\r\n", | |
"average_clicks = student_groups.mean()\r\n", | |
"\r\n", | |
"activities_and_clicks = pd.merge(activity_counts['id_site'], \\\r\n", | |
" average_clicks['sum_click'], \\\r\n", | |
" how='inner', \\\r\n", | |
" on=index_cols)\r\n", | |
" \r\n", | |
"activities_and_clicks.columns = ['Total Activities Engaged', 'Average Clicks per Activity']\r\n", | |
"activities_and_clicks.reset_index(inplace=True)\r\n", | |
"activities_and_clicks.head()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment