Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mwintjen authored Apr 2, 2020
1 parent 6ebeb2a commit 34b564e
Showing 1 changed file with 304 additions and 0 deletions.
304 changes: 304 additions & 0 deletions ch_08/notebooks/ch_08_sql_group_by.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<sqlite3.Connection at 0x27d37bc18f0>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sqlite3\n",
"sqlite3.connect('user_hits.db')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"conn = sqlite3.connect('user_hits.db') "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df_user_geo_hits = pd.read_sql_query(\"select * from tbl_user_geo_hits;\", conn)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>userid</th>\n",
" <th>date</th>\n",
" <th>city</th>\n",
" <th>state</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1/1/2019</td>\n",
" <td>Dover</td>\n",
" <td>DE</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>1/1/2019</td>\n",
" <td>El Paso</td>\n",
" <td>TX</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>1/2/2019</td>\n",
" <td>Dover</td>\n",
" <td>DE</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>1/2/2019</td>\n",
" <td>Philadelphia</td>\n",
" <td>PA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" <td>1/2/2019</td>\n",
" <td>El Paso</td>\n",
" <td>TX</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1</td>\n",
" <td>1/3/2019</td>\n",
" <td>Dover</td>\n",
" <td>DE</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>2</td>\n",
" <td>1/3/2019</td>\n",
" <td>Philadelphia</td>\n",
" <td>PA</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" userid date city state\n",
"0 1 1/1/2019 Dover DE\n",
"1 3 1/1/2019 El Paso TX\n",
"2 1 1/2/2019 Dover DE\n",
"3 2 1/2/2019 Philadelphia PA\n",
"4 3 1/2/2019 El Paso TX\n",
"5 1 1/3/2019 Dover DE\n",
"6 2 1/3/2019 Philadelphia PA"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_user_geo_hits.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df_groupby_SQL = pd.read_sql_query(\"select city, state, count(*) as hits from tbl_user_geo_hits group by 1, 2;\", conn)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>city</th>\n",
" <th>state</th>\n",
" <th>hits</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Dover</td>\n",
" <td>DE</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>El Paso</td>\n",
" <td>TX</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Philadelphia</td>\n",
" <td>PA</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" city state hits\n",
"0 Dover DE 3\n",
"1 El Paso TX 2\n",
"2 Philadelphia PA 2"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_groupby_SQL"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df_groupby_city_state = df_user_geo_hits.groupby([\"city\", \"state\"]) [\"userid\"].count() #df.groupby(\"state\")[\"last_name\"].count()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"city state\n",
"Dover DE 3\n",
"El Paso TX 2\n",
"Philadelphia PA 2\n",
"Name: userid, dtype: int64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_groupby_city_state.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"conn.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 34b564e

Please sign in to comment.