From d13abe9e2fc214294610820acc7c4458021a1ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romina=20Guti=C3=A9rrez?= Date: Wed, 13 May 2026 22:04:06 +0200 Subject: [PATCH 1/4] Solved lab --- .gitignore | 4 + python lab_sql_python_connection.ipynb | 656 +++++++++++++++++++++++++ solutions.sql | 0 3 files changed, 660 insertions(+) create mode 100644 .gitignore create mode 100644 python lab_sql_python_connection.ipynb create mode 100644 solutions.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf73cf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.ipynb_checkpoints/ +__pycache__/ +*.pyc diff --git a/python lab_sql_python_connection.ipynb b/python lab_sql_python_connection.ipynb new file mode 100644 index 0000000..fdb056b --- /dev/null +++ b/python lab_sql_python_connection.ipynb @@ -0,0 +1,656 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "b0a1e764-7cad-4676-a286-c89fb7946a52", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ok\n" + ] + } + ], + "source": [ + "import pymysql\n", + "print(\"ok\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "522f936d-cdce-4523-bbf5-ed5ffd1983de", + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy.engine import URL\n", + "from sqlalchemy import create_engine\n", + "import os\n", + "from dotenv import load_dotenv" + ] + }, + { + "cell_type": "markdown", + "id": "4e28bb67-7d08-42f8-a78f-9519b29cf2c3", + "metadata": {}, + "source": [ + "# Use password and user from SQL" + ] + }, + { + "cell_type": "markdown", + "id": "363a6f5b-6923-4572-bd12-bd2078280117", + "metadata": {}, + "source": [ + "## Establish a connection between Python and the Sakila database." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "deeeb9a6-e913-4c09-be8d-a3cffbbc7dca", + "metadata": {}, + "outputs": [], + "source": [ + "load_dotenv() #This part is to avoid sharing my password in the repo\n", + "\n", + "url = URL.create(\n", + " drivername=\"mysql+pymysql\",\n", + " username=os.getenv(\"DB_USER\"),\n", + " password=os.getenv(\"DB_PASSWORD\"), #Because of the way the password is\n", + " host=os.getenv(\"DB_HOST\"),\n", + " database=os.getenv(\"DB_NAME\")\n", + ")\n", + "\n", + "engine = create_engine(url)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4fa7e1c2-c251-465e-a93a-4d0f7c7e7141", + "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": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_test = pd.read_sql(\"SELECT * FROM rental LIMIT 5;\", engine)\n", + "df_test" + ] + }, + { + "cell_type": "markdown", + "id": "4313dc91-7d4e-4ff8-ae02-104884830b22", + "metadata": {}, + "source": [ + "## Write a Python function called rentals_month " + ] + }, + { + "cell_type": "markdown", + "id": "5b298808-ed53-43c2-8dad-e4ce8d46d5a8", + "metadata": {}, + "source": [ + "Write a Python function called rentals_month that retrieves rental data for a given month and year (passed as parameters) from the Sakila database as a Pandas DataFrame. The function should take in three parameters:\n", + "\n", + "- engine: an object representing the database connection engine to be used to establish a connection to the Sakila database.\n", + "\n", + "- month: an integer representing the month for which rental data is to be retrieved.\n", + "\n", + "- year: an integer representing the year for which rental data is to be retrieved.\n", + "\n", + "The function should execute a SQL query to retrieve the rental data for the specified month and year from the rental table in the Sakila database, and return it as a pandas DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "218d9cf8-1111-4218-9b74-0601694c4dc0", + "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", + " return pd.read_sql(query, engine)" + ] + }, + { + "cell_type": "markdown", + "id": "767598e9-51ad-4f2b-9214-c60bbf646c43", + "metadata": {}, + "source": [ + "### Test example with may 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "7c859a02-6b47-4b59-8750-b029f9d69493", + "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": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may = rentals_month(engine, 5, 2005)\n", + "may.head()" + ] + }, + { + "cell_type": "markdown", + "id": "580ee0f8-3b84-416b-ae84-ab87c1cd6bbe", + "metadata": {}, + "source": [ + "## Develop a Python function called rental_count_month " + ] + }, + { + "cell_type": "markdown", + "id": "25ddf5f8-bf6b-49d9-9f84-1a9d3ec58394", + "metadata": {}, + "source": [ + "Develop a Python function called rental_count_month that takes the DataFrame provided by rentals_month as input along with the month and year and returns a new DataFrame containing the number of rentals made by each customer_id during the selected month and year.\n", + "\n", + "The function should also include the month and year as parameters and use them to name the new column according to the month and year, for example, if the input month is 05 and the year is 2005, the column name should be \"rentals_05_2005\".\n", + "\n", + "Hint: Consider making use of pandas groupby()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "9924e680-0d89-4f1b-b11c-600876e18a40", + "metadata": {}, + "outputs": [], + "source": [ + "def rental_count_month(df, month, year):\n", + "\n", + " column_name = f\"rentals_{month:02d}_{year}\" #02d means to have 2 digits\n", + "\n", + " result = df.groupby(\"customer_id\").size().reset_index(name = column_name)\n", + " \n", + " return result" + ] + }, + { + "cell_type": "markdown", + "id": "a7d8731d-b82a-47b3-9de6-2c4a9cc3f548", + "metadata": {}, + "source": [ + "### Test example with may 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "229cc51c-1c4d-488f-bd86-7e9f7cc89d28", + "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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_counts = rental_count_month(may, 5, 2005)\n", + "may_counts.head()" + ] + }, + { + "cell_type": "markdown", + "id": "dfd68030-5d0f-4783-a37d-3aeadc0ce247", + "metadata": {}, + "source": [ + "## Create a Python function called compare_rentals " + ] + }, + { + "cell_type": "markdown", + "id": "6082ca73-48f6-42af-a162-e3d535ee8f2c", + "metadata": {}, + "source": [ + "Create a Python function called compare_rentals that takes two DataFrames as input containing the number of rentals made by each customer in different months and years. \n", + "\n", + "The function should return a combined DataFrame with a new 'difference' column, which is the difference between the number of rentals in the two months." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "4b68df15-dc33-45f7-87bb-080bd9254640", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df1, df2):\n", + " comparison = pd.merge(df1, df2, on=\"customer_id\",how=\"outer\").fillna(0)\n", + "\n", + " col1 = df1.columns[1]\n", + " col2 = df2.columns[1]\n", + "\n", + " comparison[\"diference\"]= comparison[col2] - comparison[col1]\n", + " \n", + "\n", + " return comparison" + ] + }, + { + "cell_type": "markdown", + "id": "a6b21889-5883-411c-9cb7-f4afa78c754f", + "metadata": {}, + "source": [ + "### Test example with may 2005 vs june 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3406224e-166b-4885-bfe2-3066fd2a330f", + "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_2005diference
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 rentals_05_2005 rentals_06_2005 diference\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": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may = rentals_month(engine, 5, 2005)\n", + "june = rentals_month(engine, 6, 2005)\n", + "\n", + "may_counts = rental_count_month(may, 5, 2005)\n", + "june_counts = rental_count_month(june, 6, 2005)\n", + "\n", + "comparison = compare_rentals(may_counts, june_counts)\n", + "\n", + "comparison.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3925c6f2-f1cf-42e7-8479-b56e46b66564", + "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 +} diff --git a/solutions.sql b/solutions.sql new file mode 100644 index 0000000..e69de29 From 3a76ce64e614ccb9bcb8e18f6b6803a240e56742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romina=20Guti=C3=A9rrez?= Date: Wed, 13 May 2026 22:09:07 +0200 Subject: [PATCH 2/4] Fix python connection lab --- ...n.ipynb => lab_sql_python_connection.ipynb | 0 lab_sql_python_connection.py | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+) rename python lab_sql_python_connection.ipynb => lab_sql_python_connection.ipynb (100%) create mode 100644 lab_sql_python_connection.py diff --git a/python lab_sql_python_connection.ipynb b/lab_sql_python_connection.ipynb similarity index 100% rename from python lab_sql_python_connection.ipynb rename to lab_sql_python_connection.ipynb diff --git a/lab_sql_python_connection.py b/lab_sql_python_connection.py new file mode 100644 index 0000000..8c19636 --- /dev/null +++ b/lab_sql_python_connection.py @@ -0,0 +1,54 @@ +import os +import pandas as pd +from dotenv import load_dotenv +from sqlalchemy import create_engine +from sqlalchemy.engine import URL + +load_dotenv() + +url = URL.create( + drivername="mysql+pymysql", + username=os.getenv("DB_USER"), + password=os.getenv("DB_PASSWORD"), + host=os.getenv("DB_HOST"), + database=os.getenv("DB_NAME") +) + +engine = create_engine(url) + + +def rentals_month(engine, month, year): + query = f""" + SELECT * + FROM rental + WHERE MONTH(rental_date) = {month} + AND YEAR(rental_date) = {year}; + """ + + return pd.read_sql(query, engine) + + +def rental_count_month(df, month, year): + column_name = f"rentals_{month:02d}_{year}" + + return ( + df.groupby("customer_id") + .size() + .reset_index(name=column_name) + ) + + +def compare_rentals(df1, df2): + comparison = pd.merge( + df1, + df2, + on="customer_id", + how="outer" + ).fillna(0) + + col1 = df1.columns[1] + col2 = df2.columns[1] + + comparison["difference"] = comparison[col2] - comparison[col1] + + return comparison \ No newline at end of file From f5c248849e31f2c2ec08dfe7444d2eec67b2afe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romina=20Guti=C3=A9rrez?= Date: Sat, 16 May 2026 13:09:01 +0200 Subject: [PATCH 3/4] Remove extra notebook --- lab_sql_python_connection.ipynb | 145 ++++++++++++++++++-------------- lab_sql_python_connection.py | 54 ------------ 2 files changed, 83 insertions(+), 116 deletions(-) delete mode 100644 lab_sql_python_connection.py diff --git a/lab_sql_python_connection.ipynb b/lab_sql_python_connection.ipynb index fdb056b..d9c80f7 100644 --- a/lab_sql_python_connection.ipynb +++ b/lab_sql_python_connection.ipynb @@ -1,79 +1,99 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "4e28bb67-7d08-42f8-a78f-9519b29cf2c3", + "metadata": {}, + "source": [ + "# Use password and user from SQL" + ] + }, + { + "cell_type": "markdown", + "id": "363a6f5b-6923-4572-bd12-bd2078280117", + "metadata": {}, + "source": [ + "## Establish a connection between Python and the Sakila database." + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "id": "b0a1e764-7cad-4676-a286-c89fb7946a52", "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ok\n" - ] - } - ], + "outputs": [], "source": [ + "import pandas as pd\n", + "import numpy as np\n", "import pymysql\n", - "print(\"ok\")" + "from sqlalchemy import create_engine\n", + "import getpass" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "id": "522f936d-cdce-4523-bbf5-ed5ffd1983de", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + " ········\n" + ] + } + ], "source": [ - "from sqlalchemy.engine import URL\n", - "from sqlalchemy import create_engine\n", - "import os\n", - "from dotenv import load_dotenv" + "password = getpass.getpass()" ] }, { - "cell_type": "markdown", - "id": "4e28bb67-7d08-42f8-a78f-9519b29cf2c3", + "cell_type": "code", + "execution_count": 3, + "id": "5a526192-f227-4def-8157-90e2528d6923", "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Use password and user from SQL" + "bd = \"sakila\"\n", + "\n", + "connection_string = (\n", + " 'mysql+pymysql://root:' \n", + " + password \n", + " + '@localhost/' \n", + " + bd\n", + ")\n", + "\n", + "engine = create_engine(connection_string)\n", + "\n", + "engine" ] }, { "cell_type": "markdown", - "id": "363a6f5b-6923-4572-bd12-bd2078280117", + "id": "15d7a164-f191-4db0-98d0-a5a2bddff82b", "metadata": {}, "source": [ - "## Establish a connection between Python and the Sakila database." + "### Double checking if it worked" ] }, { "cell_type": "code", - "execution_count": 8, - "id": "deeeb9a6-e913-4c09-be8d-a3cffbbc7dca", - "metadata": {}, - "outputs": [], - "source": [ - "load_dotenv() #This part is to avoid sharing my password in the repo\n", - "\n", - "url = URL.create(\n", - " drivername=\"mysql+pymysql\",\n", - " username=os.getenv(\"DB_USER\"),\n", - " password=os.getenv(\"DB_PASSWORD\"), #Because of the way the password is\n", - " host=os.getenv(\"DB_HOST\"),\n", - " database=os.getenv(\"DB_NAME\")\n", - ")\n", - "\n", - "engine = create_engine(url)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "4fa7e1c2-c251-465e-a93a-4d0f7c7e7141", + "execution_count": 4, + "id": "202c7672-c08a-4926-b8e6-ff6b57e25318", "metadata": {}, "outputs": [ { @@ -177,13 +197,14 @@ "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 " ] }, - "execution_count": 9, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_test = pd.read_sql(\"SELECT * FROM rental LIMIT 5;\", engine)\n", + "\n", "df_test" ] }, @@ -213,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 5, "id": "218d9cf8-1111-4218-9b74-0601694c4dc0", "metadata": {}, "outputs": [], @@ -240,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 6, "id": "7c859a02-6b47-4b59-8750-b029f9d69493", "metadata": {}, "outputs": [ @@ -345,7 +366,7 @@ "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 " ] }, - "execution_count": 11, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -377,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 7, "id": "9924e680-0d89-4f1b-b11c-600876e18a40", "metadata": {}, "outputs": [], @@ -401,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 8, "id": "229cc51c-1c4d-488f-bd86-7e9f7cc89d28", "metadata": {}, "outputs": [ @@ -469,7 +490,7 @@ "4 6 3" ] }, - "execution_count": 16, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -499,7 +520,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 9, "id": "4b68df15-dc33-45f7-87bb-080bd9254640", "metadata": {}, "outputs": [], @@ -510,7 +531,7 @@ " col1 = df1.columns[1]\n", " col2 = df2.columns[1]\n", "\n", - " comparison[\"diference\"]= comparison[col2] - comparison[col1]\n", + " comparison[\"difference\"]= comparison[col2] - comparison[col1]\n", " \n", "\n", " return comparison" @@ -526,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 10, "id": "3406224e-166b-4885-bfe2-3066fd2a330f", "metadata": {}, "outputs": [ @@ -554,7 +575,7 @@ " customer_id\n", " rentals_05_2005\n", " rentals_06_2005\n", - " diference\n", + " difference\n", " \n", " \n", " \n", @@ -598,15 +619,15 @@ "" ], "text/plain": [ - " customer_id rentals_05_2005 rentals_06_2005 diference\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" + " 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": 18, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } diff --git a/lab_sql_python_connection.py b/lab_sql_python_connection.py deleted file mode 100644 index 8c19636..0000000 --- a/lab_sql_python_connection.py +++ /dev/null @@ -1,54 +0,0 @@ -import os -import pandas as pd -from dotenv import load_dotenv -from sqlalchemy import create_engine -from sqlalchemy.engine import URL - -load_dotenv() - -url = URL.create( - drivername="mysql+pymysql", - username=os.getenv("DB_USER"), - password=os.getenv("DB_PASSWORD"), - host=os.getenv("DB_HOST"), - database=os.getenv("DB_NAME") -) - -engine = create_engine(url) - - -def rentals_month(engine, month, year): - query = f""" - SELECT * - FROM rental - WHERE MONTH(rental_date) = {month} - AND YEAR(rental_date) = {year}; - """ - - return pd.read_sql(query, engine) - - -def rental_count_month(df, month, year): - column_name = f"rentals_{month:02d}_{year}" - - return ( - df.groupby("customer_id") - .size() - .reset_index(name=column_name) - ) - - -def compare_rentals(df1, df2): - comparison = pd.merge( - df1, - df2, - on="customer_id", - how="outer" - ).fillna(0) - - col1 = df1.columns[1] - col2 = df2.columns[1] - - comparison["difference"] = comparison[col2] - comparison[col1] - - return comparison \ No newline at end of file From b49655a9c9dd8f5862cad8b5d6b02d3c9713fa77 Mon Sep 17 00:00:00 2001 From: romsx111 Date: Sat, 16 May 2026 13:18:50 +0200 Subject: [PATCH 4/4] Delete solutions.sql --- solutions.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 solutions.sql diff --git a/solutions.sql b/solutions.sql deleted file mode 100644 index e69de29..0000000