Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions lab-sql-python-connection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"id": "e63e6101",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import pymysql\n",
"from sqlalchemy import create_engine\n",
"import getpass\n",
"import urllib.parse\n",
"password = getpass.getpass()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3faf64c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Engine(mysql+pymysql://root:***@localhost:3306/sakila)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bd = \"sakila\"\n",
"\n",
"password_encoded = urllib.parse.quote(password)\n",
"\n",
"\n",
"connection_string = f\"mysql+pymysql://root:{password_encoded}@localhost:3306/{bd}\"\n",
"\n",
"engine = create_engine(connection_string)\n",
"\n",
"engine"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e67bca96",
"metadata": {},
"outputs": [],
"source": [
"def rentals_month(engine, month: int, year: int) -> pd.DataFrame:\n",
" query = text(\"\"\"\n",
" SELECT\n",
" rental_id,\n",
" rental_date,\n",
" inventory_id,\n",
" customer_id,\n",
" staff_id\n",
" FROM rental\n",
" WHERE EXTRACT(MONTH FROM rental_date) = :month\n",
" AND EXTRACT(YEAR FROM rental_date) = :year\n",
" \"\"\")\n",
"\n",
" df = pd.read_sql(query, engine, params={\"month\": month, \"year\": year})\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "41097caf",
"metadata": {},
"outputs": [],
"source": [
"def rental_count_month(df: pd.DataFrame, month: int, year: int) -> pd.DataFrame:\n",
" col_name = f\"rentals_{month:02d}_{year}\"\n",
" \n",
" counts = (\n",
" df.groupby(\"customer_id\")\n",
" .size()\n",
" .reset_index(name=col_name)\n",
" )\n",
"\n",
" return counts"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c9719ccb",
"metadata": {},
"outputs": [],
"source": [
"def compare_rentals(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:\n",
" merged = pd.merge(df1, df2, on=\"customer_id\", how=\"outer\")\n",
"\n",
" merged = merged.fillna(0)\n",
" count_cols = [c for c in merged.columns if c != \"customer_id\"]\n",
" if len(count_cols) != 2:\n",
" raise ValueError(\n",
" f\"Expected two rental count columns, but got:{count_cols}\"\n",
" )\n",
"\n",
" col1, col2 = count_cols\n",
"\n",
" merged[\"difference\"] = merged[col2] - merged[col1]\n",
"\n",
" return merged"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9260daa4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" customer_id rentals_05_2005 rentals_06_2005 difference\n",
"0 1 2.0 7.0 5.0\n",
"1 2 1.0 1.0 0.0\n",
"2 3 2.0 4.0 2.0\n",
"3 4 0.0 6.0 6.0\n",
"4 5 3.0 5.0 2.0\n"
]
}
],
"source": [
"\n",
"may_df = rentals_month(engine, 5, 2005)\n",
"june_df = rentals_month(engine, 6, 2005)\n",
"\n",
"may_counts = rental_count_month(may_df, 5, 2005)\n",
"june_counts = rental_count_month(june_df, 6, 2005)\n",
"\n",
"comparison = compare_rentals(may_counts, june_counts)\n",
"print(comparison.head())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc86b70",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}