From 96f28af54b261681e72831a74aeea6e8a0c49f11 Mon Sep 17 00:00:00 2001 From: SofiaPS-bio Date: Sat, 8 Nov 2025 14:48:18 +0100 Subject: [PATCH] Solved lab --- Solved_lab.ipynb | 598 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 598 insertions(+) create mode 100644 Solved_lab.ipynb diff --git a/Solved_lab.ipynb b/Solved_lab.ipynb new file mode 100644 index 0000000..2272bcc --- /dev/null +++ b/Solved_lab.ipynb @@ -0,0 +1,598 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "8c875bbd", + "metadata": {}, + "outputs": [], + "source": [ + "# Establish a connection between Python and the Sakila database.\n", + "\n", + "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": 3, + "id": "8a7624dd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 3, + "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": "code", + "execution_count": 4, + "id": "b8f2298c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlalchemy import text\n", + "\n", + "with engine.connect() as connection:\n", + " query = text('SELECT * FROM film')\n", + " result = connection.execute(query)\n", + "\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9beda96e", + "metadata": {}, + "outputs": [], + "source": [ + "# Write a Python function called rentals_month that retrieves rental data for a given month and year (passed as parameters)\n", + "# from the Sakila database as a Pandas DataFrame. The function should take in three parameters:\n", + "# engine: an object representing the database connection engine to be used to establish a connection to the Sakila database.\n", + "# month: an integer representing the month for which rental data is to be retrieved.\n", + "# year: an integer representing the year for which rental data is to be retrieved.\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,\n", + "# and return it as a pandas DataFrame.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "97d306e9", + "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", + " \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
011582005-06-14 22:53:3316324162005-06-18 21:37:3322006-02-15 21:30:53
111592005-06-14 22:55:1343955162005-06-17 02:11:1312006-02-15 21:30:53
211602005-06-14 23:00:3427952392005-06-18 01:58:3422006-02-15 21:30:53
311612005-06-14 23:07:0816902852005-06-21 17:12:0812006-02-15 21:30:53
411622005-06-14 23:09:389873102005-06-23 22:00:3812006-02-15 21:30:53
........................
230634652005-06-21 22:10:0114885102005-06-30 21:35:0112006-02-15 21:30:53
230734662005-06-21 22:13:333712262005-06-25 21:01:3322006-02-15 21:30:53
230834672005-06-21 22:19:257295432005-06-27 00:03:2522006-02-15 21:30:53
230934682005-06-21 22:43:4528991002005-06-30 01:49:4512006-02-15 21:30:53
231034692005-06-21 22:48:5940871812005-06-28 19:32:5912006-02-15 21:30:53
\n", + "

2311 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1158 2005-06-14 22:53:33 1632 416 \n", + "1 1159 2005-06-14 22:55:13 4395 516 \n", + "2 1160 2005-06-14 23:00:34 2795 239 \n", + "3 1161 2005-06-14 23:07:08 1690 285 \n", + "4 1162 2005-06-14 23:09:38 987 310 \n", + "... ... ... ... ... \n", + "2306 3465 2005-06-21 22:10:01 1488 510 \n", + "2307 3466 2005-06-21 22:13:33 371 226 \n", + "2308 3467 2005-06-21 22:19:25 729 543 \n", + "2309 3468 2005-06-21 22:43:45 2899 100 \n", + "2310 3469 2005-06-21 22:48:59 4087 181 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-06-18 21:37:33 2 2006-02-15 21:30:53 \n", + "1 2005-06-17 02:11:13 1 2006-02-15 21:30:53 \n", + "2 2005-06-18 01:58:34 2 2006-02-15 21:30:53 \n", + "3 2005-06-21 17:12:08 1 2006-02-15 21:30:53 \n", + "4 2005-06-23 22:00:38 1 2006-02-15 21:30:53 \n", + "... ... ... ... \n", + "2306 2005-06-30 21:35:01 1 2006-02-15 21:30:53 \n", + "2307 2005-06-25 21:01:33 2 2006-02-15 21:30:53 \n", + "2308 2005-06-27 00:03:25 2 2006-02-15 21:30:53 \n", + "2309 2005-06-30 01:49:45 1 2006-02-15 21:30:53 \n", + "2310 2005-06-28 19:32:59 1 2006-02-15 21:30:53 \n", + "\n", + "[2311 rows x 7 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def rentals_month(engine, year, month):\n", + " with engine.connect() as connection:\n", + " query = text(f'select * from rental where rental_date like \"{year}-{month}%\"')\n", + " result = connection.execute(query)\n", + " df = pd.DataFrame(result.all())\n", + " return df\n", + " \n", + "df_rentals = rentals_month(engine, \"2005\", \"06\")\n", + "\n", + "df_rentals" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "42006fbe", + "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", + "
customer_idrentals_06_2005
017
121
234
346
455
.........
5855952
5865962
5875973
5885981
5895994
\n", + "

590 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_06_2005\n", + "0 1 7\n", + "1 2 1\n", + "2 3 4\n", + "3 4 6\n", + "4 5 5\n", + ".. ... ...\n", + "585 595 2\n", + "586 596 2\n", + "587 597 3\n", + "588 598 1\n", + "589 599 4\n", + "\n", + "[590 rows x 2 columns]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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\n", + "# a new DataFrame containing the number of rentals made by each customer_id during the selected month and year.\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,\n", + "# if the input month is 05 and the year is 2005, the column name should be \"rentals_05_2005\".\n", + "# Hint: Consider making use of pandas groupby()\n", + "\n", + "def rental_count_month(rentals_month, year, month):\n", + " rental_counts = rentals_month.groupby(\"customer_id\").size().reset_index(name=f\"rentals_{month}_{year}\")\n", + " return rental_counts\n", + "\n", + "df_counts = rental_count_month(rentals_month(engine, \"2005\", \"06\"), \"2005\", \"06\")\n", + "\n", + "df_counts" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "dc5ce096", + "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", + " \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_07_2005rentals_06_2005difference
01127.0-5.0
12141.0-13.0
23134.0-9.0
3456.01.0
45165.0-11.0
...............
594595192.0-17.0
59559662.0-4.0
59659773.0-4.0
597598161.0-15.0
59859974.0-3.0
\n", + "

599 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_07_2005 rentals_06_2005 difference\n", + "0 1 12 7.0 -5.0\n", + "1 2 14 1.0 -13.0\n", + "2 3 13 4.0 -9.0\n", + "3 4 5 6.0 1.0\n", + "4 5 16 5.0 -11.0\n", + ".. ... ... ... ...\n", + "594 595 19 2.0 -17.0\n", + "595 596 6 2.0 -4.0\n", + "596 597 7 3.0 -4.0\n", + "597 598 16 1.0 -15.0\n", + "598 599 7 4.0 -3.0\n", + "\n", + "[599 rows x 4 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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\n", + "# and years. 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.\n", + "\n", + "\n", + "def compare_rentals(df1, df2):\n", + " df_merged = pd.merge(df1, df2, on=\"customer_id\", how=\"outer\").fillna(0)\n", + "\n", + " # Identify the two rental columns dynamically\n", + " rental_cols = [col for col in df_merged.columns if col.startswith(\"rentals_\")]\n", + "\n", + " # Compute the difference between the two months\n", + " df_merged[\"difference\"] = df_merged[rental_cols[1]] - df_merged[rental_cols[0]]\n", + "\n", + " return df_merged\n", + "\n", + "final = compare_rentals(rental_count_month(rentals_month(engine, \"2005\", \"07\"), \"2005\", \"07\"), rental_count_month(rentals_month(engine, \"2005\", \"06\"), \"2005\", \"06\"))\n", + "\n", + "final\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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}