Skip to content
Open
Show file tree
Hide file tree
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
295 changes: 295 additions & 0 deletions .ipynb_checkpoints/lab-sql-python-connection-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "53022ce9-0fe0-4039-8c67-27538ef9c821",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pymysql in c:\\users\\kraus\\anaconda3\\lib\\site-packages (1.2.0)\n"
]
}
],
"source": [
"!pip install pymysql"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "789390e3-97ec-431a-986d-bd7164d389a5",
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy import create_engine\n",
"\n",
"engine = create_engine('mysql+pymysql://root:Ironhacksummer2026!@localhost/sakila')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2e114ebd-c6c7-4dd7-b597-651e12e9440f",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"def rentals_month(engine, month, year):\n",
" query = f\"\"\"\n",
" SELECT * FROM rental\n",
" WHERE MONTH(rental_date) = {month} AND YEAR(rental_date) = {year}\n",
" \"\"\"\n",
" df = pd.read_sql(query, engine)\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f0f65640-ae5c-472e-b675-6794f6858f8f",
"metadata": {},
"outputs": [],
"source": [
"def rental_count_month(df, month, year):\n",
" counts = df.groupby('customer_id').size().reset_index(name=f'rentals_{month:02d}_{year}')\n",
" return counts"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9682820f-eed2-4dc1-9280-4bb852973bd2",
"metadata": {},
"outputs": [],
"source": [
"def compare_rentals(df1, df2):\n",
" merged = pd.merge(df1, df2, on='customer_id', how='outer')\n",
" merged['difference'] = merged.iloc[:, 1] - merged.iloc[:, 2]\n",
" return merged"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ee55df88-0a9b-4855-a8f9-2286e599ea23",
"metadata": {},
"outputs": [],
"source": [
"may_df = rentals_month(engine, 5, 2005)\n",
"june_df = rentals_month(engine, 6, 2005)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5ba6da4a-c516-469d-badb-16ffcaf2901c",
"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>customer_id</th>\n",
" <th>rentals_06_2005_x</th>\n",
" <th>rentals_06_2005_y</th>\n",
" <th>difference</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>7</td>\n",
" <td>7</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>5</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>585</th>\n",
" <td>595</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>586</th>\n",
" <td>596</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>587</th>\n",
" <td>597</td>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>588</th>\n",
" <td>598</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>589</th>\n",
" <td>599</td>\n",
" <td>4</td>\n",
" <td>4</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>590 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" customer_id rentals_06_2005_x rentals_06_2005_y difference\n",
"0 1 7 7 0\n",
"1 2 1 1 0\n",
"2 3 4 4 0\n",
"3 4 6 6 0\n",
"4 5 5 5 0\n",
".. ... ... ... ...\n",
"585 595 2 2 0\n",
"586 596 2 2 0\n",
"587 597 3 3 0\n",
"588 598 1 1 0\n",
"589 599 4 4 0\n",
"\n",
"[590 rows x 4 columns]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"may_counts = rental_count_month(june_df, 6, 2005)\n",
"june_counts = rental_count_month(june_df, 6, 2005)\n",
"\n",
"result = compare_rentals(may_counts, june_counts)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17f2be36-b8a4-4e05-9135-484215470705",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "aef1fa62-fcd2-4a24-8fe3-373129346735",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f26136db-0d4e-4e32-ad42-09c25c0cec70",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d33cd019-9e3a-433f-b709-ce4dfef79310",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "60e39851-b6e3-4a07-b77a-b4931b09f22e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading