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
314 changes: 314 additions & 0 deletions lab_sql_python_connection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4212c555",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine\n",
"import getpass\n",
"from urllib.parse import quote_plus\n",
"\n",
"password = quote_plus(getpass.getpass())\n",
"\n",
"engine = create_engine(f\"mysql+pymysql://root:{password}@localhost/sakila\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6fe918ee",
"metadata": {},
"outputs": [],
"source": [
"def rentals_month(engine, month, year):\n",
" \n",
" query = f\"\"\"\n",
" SELECT *\n",
" FROM rental\n",
" WHERE MONTH(rental_date) = {month}\n",
" AND YEAR(rental_date) = {year}\n",
" \"\"\"\n",
" \n",
" df = pd.read_sql(query, engine)\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0ff1a2ae",
"metadata": {},
"outputs": [],
"source": [
"def rental_count_month(df, month, year):\n",
" \n",
" result = (\n",
" df.groupby('customer_id')\n",
" .size()\n",
" .reset_index(name=f\"rentals_{month:02d}_{year}\")\n",
" )\n",
" \n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1cfda834",
"metadata": {},
"outputs": [],
"source": [
"def compare_rentals(df1, df2):\n",
" \n",
" merged = pd.merge(df1, df2, on='customer_id', how='outer').fillna(0)\n",
" \n",
" col1 = df1.columns[1]\n",
" col2 = df2.columns[1]\n",
" \n",
" merged['difference'] = merged[col2] - merged[col1]\n",
" \n",
" return merged"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c9147801",
"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_05_2005</th>\n",
" <th>rentals_06_2005</th>\n",
" <th>difference</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2.0</td>\n",
" <td>7.0</td>\n",
" <td>5.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>0.0</td>\n",
" <td>6.0</td>\n",
" <td>6.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>3.0</td>\n",
" <td>5.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 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"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mayo\n",
"may_df = rentals_month(engine, 5, 2005)\n",
"may_counts = rental_count_month(may_df, 5, 2005)\n",
"\n",
"# Junio\n",
"jun_df = rentals_month(engine, 6, 2005)\n",
"jun_counts = rental_count_month(jun_df, 6, 2005)\n",
"\n",
"# Comparación\n",
"comparison = compare_rentals(may_counts, jun_counts)\n",
"\n",
"comparison.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c1112890",
"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_05_2005</th>\n",
" <th>rentals_06_2005</th>\n",
" <th>difference</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>31</td>\n",
" <td>0.0</td>\n",
" <td>11.0</td>\n",
" <td>11.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>452</th>\n",
" <td>454</td>\n",
" <td>1.0</td>\n",
" <td>10.0</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>327</th>\n",
" <td>329</td>\n",
" <td>0.0</td>\n",
" <td>9.0</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>211</th>\n",
" <td>213</td>\n",
" <td>1.0</td>\n",
" <td>9.0</td>\n",
" <td>8.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>177</th>\n",
" <td>178</td>\n",
" <td>0.0</td>\n",
" <td>8.0</td>\n",
" <td>8.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" customer_id rentals_05_2005 rentals_06_2005 difference\n",
"30 31 0.0 11.0 11.0\n",
"452 454 1.0 10.0 9.0\n",
"327 329 0.0 9.0 9.0\n",
"211 213 1.0 9.0 8.0\n",
"177 178 0.0 8.0 8.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comparison.sort_values('difference', ascending=False).head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "17fb4b73",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Conclusión:\n",
"Los clientes con diferencia positiva alquilaron más en junio que en mayo.\n",
"Los negativos redujeron su actividad.\n"
]
}
],
"source": [
"print(\"Conclusión:\")\n",
"print(\"Los clientes con diferencia positiva alquilaron más en junio que en mayo.\")\n",
"print(\"Los negativos redujeron su actividad.\")"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}