diff --git a/LAB CONNECTION.ipynb b/LAB CONNECTION.ipynb new file mode 100644 index 0000000..b15e244 --- /dev/null +++ b/LAB CONNECTION.ipynb @@ -0,0 +1,538 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b5d52eb9", + "metadata": {}, + "source": [ + "#REALIZAMOS CONEXION DE PYTHON CON BASE DE DATOS SAKILA" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "34542a84", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Version: 2.0.46\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip show sqlalchemy | grep Version" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "fcfd378d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pymysql in /opt/anaconda3/lib/python3.13/site-packages (1.1.2)\n", + "Note: you may need to restart the kernel to use updated packages.\n", + "Requirement already satisfied: sqlalchemy in /opt/anaconda3/lib/python3.13/site-packages (2.0.46)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in /opt/anaconda3/lib/python3.13/site-packages (from sqlalchemy) (4.15.0)\n" + ] + } + ], + "source": [ + "%pip install pymysql\n", + "!pip install --upgrade sqlalchemy" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cbeec41a", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6fb7517b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bd = \"sakila\"\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/'+bd\n", + "engine = create_engine(connection_string)\n", + "engine" + ] + }, + { + "cell_type": "markdown", + "id": "2f7e2ae7", + "metadata": {}, + "source": [ + "#CREAMOS LA FUNCION PARA ACCEDER A LOS ALQUILERES MENSUALES#" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "145e4cdd", + "metadata": {}, + "outputs": [], + "source": [ + "def rentals_month(engine, month, year):\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\n" + ] + }, + { + "cell_type": "markdown", + "id": "c8e2ff1b", + "metadata": {}, + "source": [ + "##CREAMOS LA FUNCION PARA ACCEDER A LOS ALQUILERES DEL MES DE MAYO, TAL COMO EL EJEMPLO MENCIONADO EN EL EJERCICIO" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e704850a", + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
012005-05-24 22:53:303671302005-05-26 22:04:3012006-02-15 21:30:53
122005-05-24 22:54:3315254592005-05-28 19:40:3312006-02-15 21:30:53
232005-05-24 23:03:3917114082005-06-01 22:12:3912006-02-15 21:30:53
342005-05-24 23:04:4124523332005-06-03 01:43:4122006-02-15 21:30:53
452005-05-24 23:05:2120792222005-06-02 04:33:2112006-02-15 21:30:53
\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1 2005-05-24 22:53:30 367 130 \n", + "1 2 2005-05-24 22:54:33 1525 459 \n", + "2 3 2005-05-24 23:03:39 1711 408 \n", + "3 4 2005-05-24 23:04:41 2452 333 \n", + "4 5 2005-05-24 23:05:21 2079 222 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-05-26 22:04:30 1 2006-02-15 21:30:53 \n", + "1 2005-05-28 19:40:33 1 2006-02-15 21:30:53 \n", + "2 2005-06-01 22:12:39 1 2006-02-15 21:30:53 \n", + "3 2005-06-03 01:43:41 2 2006-02-15 21:30:53 \n", + "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 " + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_rentals = rentals_month(engine, 5, 2005)\n", + "may_rentals.head()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "18811967", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1156, 7)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_rentals.shape\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "21846a95", + "metadata": {}, + "outputs": [], + "source": [ + "def conteo_mensual_renta(df, month, year):\n", + " column_name = f\"rentals_{month:02d}_{year}\"\n", + " \n", + " result = (\n", + " df\n", + " .groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=column_name)\n", + " )\n", + " \n", + " return result\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "5d98c495", + "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", + "
customer_idrentals_05_2005
012
121
232
353
463
\n", + "
" + ], + "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": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_counts = conteo_mensual_renta(may_rentals, 5, 2005)\n", + "may_counts.head()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "729ef838", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(520, 2)" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_counts.shape\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "477f9e89", + "metadata": {}, + "outputs": [], + "source": [ + "#CREAMOS LA FUNCION DE COMPARACIÓN DE RENTAS Y LA UTILIZAMOS PARA COMPARAR LA PRODUCCION DE LOS MESES DE MAYO Y JUNIO" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c7fc96fe", + "metadata": {}, + "outputs": [], + "source": [ + "def comparación_rentas(df1, df2):\n", + " merged = pd.merge(df1, df2, on=\"customer_id\", how=\"inner\")\n", + " \n", + " col1 = df1.columns[1]\n", + " col2 = df2.columns[1]\n", + " \n", + " merged[\"Diferencia entre meses\"] = merged[col2] - merged[col1]\n", + " \n", + " return merged\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "e834bf74", + "metadata": {}, + "outputs": [], + "source": [ + "jun_rentals = rentals_month(engine, 6, 2005)\n", + "jun_counts = conteo_mensual_renta(jun_rentals, 6, 2005)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "cdcb56fd", + "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_idrentals_05_2005rentals_06_2005Diferencia entre meses
01275
12110
23242
35352
46341
\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_06_2005 Diferencia entre meses\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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comparacion = comparación_rentas(may_counts, jun_counts)\n", + "comparacion.head()\n" + ] + } + ], + "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 +}