From 3277eb32bb4abc77a6b8c273849930b69a371e3f Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 11 Jul 2026 10:12:12 +0100 Subject: [PATCH] Lab 7 of unit 9 done --- Lab7.ipynb | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 Lab7.ipynb diff --git a/Lab7.ipynb b/Lab7.ipynb new file mode 100644 index 0000000..eaebea8 --- /dev/null +++ b/Lab7.ipynb @@ -0,0 +1,219 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "421a578c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: sqlalchemy in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (2.0.51)\n", + "Requirement already satisfied: pandas in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (3.0.3)\n", + "Requirement already satisfied: greenlet>=1 in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (from sqlalchemy) (3.3.2)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (from sqlalchemy) (4.15.0)\n", + "Requirement already satisfied: numpy>=1.26.0 in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (from pandas) (2.4.6)\n", + "Requirement already satisfied: python-dateutil>=2.8.2 in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (from pandas) (2.9.0.post0)\n", + "Requirement already satisfied: six>=1.5 in /home/ricci-preto/anaconda3/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install sqlalchemy pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "940c0fd4", + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import create_engine\n", + "import pandas as pd\n", + "import os\n", + "from dotenv import load_dotenv" + ] + }, + { + "cell_type": "markdown", + "id": "9e125a11", + "metadata": {}, + "source": [ + "Establishing a Connection and Defining rentals_month" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2698fc1f", + "metadata": {}, + "outputs": [], + "source": [ + "load_dotenv()\n", + "\n", + "user = os.getenv(\"MYSQL_USER\")\n", + "password = os.getenv(\"MYSQL_PASSWORD\")\n", + "\n", + "engine = create_engine(\n", + " f\"mysql+pymysql://{user}:{password}@localhost/sakila\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "2ca289dd", + "metadata": {}, + "source": [ + "Defining rentals_month" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "7fddaac3", + "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" + ] + }, + { + "cell_type": "markdown", + "id": "9edc80ce", + "metadata": {}, + "source": [ + "Defining rental_count_month" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "eb22908e", + "metadata": {}, + "outputs": [], + "source": [ + "def rental_count_month(df, month, year):\n", + " column_name = f\"rentals_{month:02d}_{year}\"\n", + "\n", + " result = (\n", + " df.groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=column_name)\n", + " )\n", + "\n", + " return result" + ] + }, + { + "cell_type": "markdown", + "id": "c8e8602f", + "metadata": {}, + "source": [ + "Defining compare_rentals" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0bde660c", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df1, df2):\n", + " merged = pd.merge(df1, df2, on=\"customer_id\", how=\"inner\")\n", + "\n", + " rental_cols = [col for col in merged.columns if col != \"customer_id\"]\n", + "\n", + " merged[\"difference\"] = (\n", + " merged[rental_cols[1]] - merged[rental_cols[0]]\n", + " )\n", + "\n", + " return merged" + ] + }, + { + "cell_type": "markdown", + "id": "d467ba64", + "metadata": {}, + "source": [ + "Example:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "19831ca2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 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\n" + ] + } + ], + "source": [ + "# Get rentals\n", + "may_rentals = rentals_month(engine, 5, 2005)\n", + "june_rentals = rentals_month(engine, 6, 2005)\n", + "\n", + "# Count rentals\n", + "may_counts = rental_count_month(may_rentals, 5, 2005)\n", + "june_counts = rental_count_month(june_rentals, 6, 2005)\n", + "\n", + "# Compare\n", + "comparison = compare_rentals(may_counts, june_counts)\n", + "\n", + "print(comparison.head())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fd7c02d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +}