Last active
July 20, 2024 11:04
-
-
Save lucasrodes/f9a4183d769b7daacd9ea8405a5a08db to your computer and use it in GitHub Desktop.
Gist with notebook complementing my post on Medium
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook complements my blog post **Exploring WhatsApp data, Analyze your WhatsApp chat data using Python** on Medium." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Install dependencies\n", | |
"!pip install whatstk==0.4.1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Load a chat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Load chat\n", | |
"from whatstk import WhatsAppChat\n", | |
"filepath = 'http://raw.githubusercontent.com/lucasrodes/whatstk/develop/chats/whatsapp/lorem-2000.txt' # change to your chat file\n", | |
"chat = WhatsAppChat.from_source(filepath=filepath)\n", | |
"# Chat as Dataframe available in chat.df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Loading chat setting hformat manually (skip if previous cell worked)\n", | |
"# hformat = '[%y-%m-%d %H:%M] %name:' # change to hformat of your chat\n", | |
"# chat = WhatsAppChat.from_source(filepath=filepath, hformat=hformat)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## First data overview" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"First date is: 2019-04-16 02:09:00\n", | |
"Last date is: 2020-06-13 00:00:00\n" | |
] | |
} | |
], | |
"source": [ | |
"init_date = chat.df.date.min()\n", | |
"end_date = chat.df.date.max()\n", | |
"print(f\"First date is: {init_date}\\nLast date is: {end_date}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"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>message</th>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>username</th>\n", | |
" <th></th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>+1 123 456 789</th>\n", | |
" <td>507</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>Giuseppe</th>\n", | |
" <td>516</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>John</th>\n", | |
" <td>481</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>Mary</th>\n", | |
" <td>496</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" message\n", | |
"username \n", | |
"+1 123 456 789 507\n", | |
"Giuseppe 516\n", | |
"John 481\n", | |
"Mary 496" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"chat.df.groupby('username').agg('count')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"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>num_messages</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>2019-07-01</th>\n", | |
" <td>20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-07-26</th>\n", | |
" <td>20</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-07-11</th>\n", | |
" <td>19</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-08-22</th>\n", | |
" <td>18</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2020-03-23</th>\n", | |
" <td>18</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-08-23</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-05-28</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-05-02</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2019-04-18</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2020-06-13</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>235 rows × 1 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" num_messages\n", | |
"2019-07-01 20\n", | |
"2019-07-26 20\n", | |
"2019-07-11 19\n", | |
"2019-08-22 18\n", | |
"2020-03-23 18\n", | |
"... ...\n", | |
"2019-08-23 1\n", | |
"2019-05-28 1\n", | |
"2019-05-02 1\n", | |
"2019-04-18 1\n", | |
"2020-06-13 1\n", | |
"\n", | |
"[235 rows x 1 columns]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Day with the most number of messages sent\n", | |
"chat.df.groupby(chat.df.date).agg(num_messages=('message', 'count')).sort_values(by='num_messages', ascending=False)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Who talks the most?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'temp-plot.html'" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Number of messages sent\n", | |
"from whatstk import FigureBuilder\n", | |
"from plotly.offline import plot\n", | |
"fb = FigureBuilder(chat=chat)\n", | |
"fig = fb.user_interventions_count_linechart(cumulative=True, title='User inteventions count (cumulative)')\n", | |
"plot(fig)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'temp-plot.html'" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Number of characters sent\n", | |
"fig = fb.user_interventions_count_linechart(cumulative=True, msg_length=True, title='Count of sent characters (cumulative)')\n", | |
"plot(fig)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## User message length" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'temp-plot.html'" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"fig = fb.user_msg_length_boxplot()\n", | |
"plot(fig)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## User interaction" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'temp-plot.html'" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"fig = fb.user_message_responses_heatmap()\n", | |
"plot(fig)" | |
] | |
} | |
], | |
"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.8.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment