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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ git push origin master
```

- Paste the link of your lab in Student Portal.
ok



378 changes: 378 additions & 0 deletions lab sql python conncetion.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "304fdd20",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b608b101",
"metadata": {},
"outputs": [],
"source": [
"# Conectamos con nuestro MySQL\n",
"\n",
"engine = create_engine(\"mysql+pymysql://root:30802043@localhost:3306/sakila\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1f53c092",
"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>rental_id</th>\n",
" <th>customer_id</th>\n",
" <th>rental_date</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>130</td>\n",
" <td>2005-05-24 22:53:30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>459</td>\n",
" <td>2005-05-24 22:54:33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>408</td>\n",
" <td>2005-05-24 23:03:39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>333</td>\n",
" <td>2005-05-24 23:04:41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>222</td>\n",
" <td>2005-05-24 23:05:21</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" rental_id customer_id rental_date\n",
"0 1 130 2005-05-24 22:53:30\n",
"1 2 459 2005-05-24 22:54:33\n",
"2 3 408 2005-05-24 23:03:39\n",
"3 4 333 2005-05-24 23:04:41\n",
"4 5 222 2005-05-24 23:05:21"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# rentals_month, esta función recibe el \"engine\", recibe \"month\" y \"year\" y consulta la tabla \"rental\"\n",
"# Devuelve un DataFrame con los alquileres de ese mes y año\n",
"\n",
"def rentals_month(engine, month, year):\n",
" query = f\"\"\"\n",
" SELECT\n",
" rental_id,\n",
" customer_id,\n",
" rental_date\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\n",
"\n",
"may_rentals = rentals_month(engine, 5, 2005)\n",
"may_rentals.head()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d977c11b",
"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",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>5</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" customer_id rentals_05_2005\n",
"0 1 2\n",
"1 2 1\n",
"2 3 2\n",
"3 5 3\n",
"4 6 3"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# rental_count_month, esta función recibe el DataFrame anterior y cuenta cuántos alquileres hizo cada customer_id\n",
"# nombra la columna según mes y año (ej: rentals_05_2005)\n",
"\n",
"def rental_count_month(df, month, year):\n",
" column_name = f\"rentals_{str(month).zfill(2)}_{year}\"\n",
" \n",
" rentals_count = (\n",
" df.groupby(\"customer_id\")\n",
" .size()\n",
" .reset_index(name=column_name)\n",
" )\n",
" \n",
" return rentals_count\n",
"\n",
"may_count = rental_count_month(may_rentals, 5, 2005)\n",
"may_count.head()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "364a646e",
"metadata": {},
"outputs": [],
"source": [
"# compare_rentals, esta función recibe dos DataFrames (por ejemplo mayo y junio) y une ambos por customer_id\n",
"# calcula la diferencia de alquileres entre meses\n",
"\n",
"def compare_rentals(df1, df2):\n",
" df = pd.merge(df1, df2, on=\"customer_id\", how=\"inner\")\n",
" \n",
" col1 = df1.columns[1]\n",
" col2 = df2.columns[1]\n",
" \n",
" df[\"difference\"] = df[col2] - df[col1]\n",
" \n",
" return df\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1a086ac1",
"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</td>\n",
" <td>7</td>\n",
" <td>5</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>2</td>\n",
" <td>4</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>5</td>\n",
" <td>3</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" customer_id rentals_05_2005 rentals_06_2005 difference\n",
"0 1 2 7 5\n",
"1 2 1 1 0\n",
"2 3 2 4 2\n",
"3 5 3 5 2\n",
"4 6 3 4 1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mayo vs Junio 2005\n",
"\n",
"# Obtener alquileres por mes\n",
"may_rentals = rentals_month(engine, 5, 2005)\n",
"june_rentals = rentals_month(engine, 6, 2005)\n",
"\n",
"# Contar alquileres por cliente\n",
"may_count = rental_count_month(may_rentals, 5, 2005)\n",
"june_count = rental_count_month(june_rentals, 6, 2005)\n",
"\n",
"# Comparar actividad\n",
"comparison = compare_rentals(may_count, june_count)\n",
"\n",
"comparison.head()\n"
]
},
{
"cell_type": "markdown",
"id": "42b9cd20",
"metadata": {},
"source": [
"fin"
]
}
],
"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.14.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}