From f3604c2a885ed99c58473d2dd89c5a7b9e731232 Mon Sep 17 00:00:00 2001 From: Alejandro de Tuero Date: Sat, 6 Dec 2025 10:14:54 +0100 Subject: [PATCH] Solved_lab --- .gitignore | 1 + README.md | 2 +- connection.ipynb | 215 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 connection.ipynb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README.md b/README.md index b0ef389..b11157b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@

- +v
diff --git a/connection.ipynb b/connection.ipynb new file mode 100644 index 0000000..ec9d958 --- /dev/null +++ b/connection.ipynb @@ -0,0 +1,215 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "081e20bf", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import pandas as pd\n", + "from sqlalchemy import create_engine\n", + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv(\".env\") # carga tu contraseña u otros secretos\n", + "\n", + "USER = \"root\"\n", + "PASSWORD = os.getenv(\"Key\")\n", + "HOST = \"localhost\"\n", + "PORT = \"3306\"\n", + "DB_NAME = \"sakila\"\n", + "\n", + "engine = create_engine(f\"mysql+pymysql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DB_NAME}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3ba4755", + "metadata": {}, + "outputs": [], + "source": [ + "# Funcion rentals month\n", + "def rentals_month(engine, month, year):\n", + " \"\"\"\n", + " Recupera todos los alquileres del mes y año especificados.\n", + " \"\"\"\n", + " query = f\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " WHERE MONTH(rental_date) = {month}\n", + " AND YEAR(rental_date) = {year};\n", + " \"\"\"\n", + " df = pd.read_sql(query, engine)\n", + " return df\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e2a2926", + "metadata": {}, + "outputs": [], + "source": [ + "# Funcion rental_count_month\n", + "def rental_count_month(df, month, year):\n", + " \"\"\"\n", + " Devuelve un DataFrame con el número total de alquileres por cliente.\n", + " \"\"\"\n", + " col_name = f\"alquileres_{month:02d}_{year}\"\n", + "\n", + " df_counts = (\n", + " df.groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=col_name)\n", + " )\n", + "\n", + " return df_counts\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6263cb5f", + "metadata": {}, + "outputs": [], + "source": [ + "# Funcion compare_rentals\n", + "def compare_rentals(df1, df2):\n", + " \"\"\"\n", + " Combina dos DataFrames de conteo de alquileres por cliente\n", + " y calcula la diferencia entre ambos meses.\n", + " \"\"\"\n", + " df_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", + " df_merged[\"diferencia\"] = df_merged[col2] - df_merged[col1]\n", + "\n", + " return df_merged\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91fba459", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idalquileres_05_2005alquileres_06_2005diferencia
012.07.05.0
121.01.00.0
232.04.02.0
340.06.06.0
453.05.02.0
\n", + "
" + ], + "text/plain": [ + " customer_id alquileres_05_2005 alquileres_06_2005 diferencia\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": [ + "# Check\n", + "df_may = rentals_month(engine, 5, 2005)\n", + "df_jun = rentals_month(engine, 6, 2005)\n", + "\n", + "count_may = rental_count_month(df_may, 5, 2005)\n", + "count_jun = rental_count_month(df_jun, 6, 2005)\n", + "\n", + "comparison = compare_rentals(count_may, count_jun)\n", + "comparison.head()\n" + ] + } + ], + "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.12.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}