From d64aa4cbdefba855cc0328702c83cd3265fd3633 Mon Sep 17 00:00:00 2001 From: claireley Date: Fri, 29 May 2026 16:19:00 +0200 Subject: [PATCH 1/2] solved lab --- SQL-Python-connection.ipynb | 572 ++++++++++++++++++++++++++++++++++++ 1 file changed, 572 insertions(+) create mode 100644 SQL-Python-connection.ipynb diff --git a/SQL-Python-connection.ipynb b/SQL-Python-connection.ipynb new file mode 100644 index 0000000..fd3bf54 --- /dev/null +++ b/SQL-Python-connection.ipynb @@ -0,0 +1,572 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "c056edcd-27ac-4c24-ac07-4196bb0b10eb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please, input your SQL password: ········\n" + ] + }, + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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(\"Please, input your SQL password: \")\n", + "\n", + "# Note that when you use _SQLAlchemy_ and establish the connection, you do not even need to be logged in SQL Pro or MySQL Workbench.\n", + "bd = \"sakila\"\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/'+bd\n", + "engine = create_engine(connection_string)\n", + "engine" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "971abdbb-258d-455a-8b18-dbcd50955abf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlalchemy import text" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "50ab132b-3d17-4dac-991b-43597090b5b2", + "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
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
........................
115111532005-05-31 21:36:4427255062005-06-10 01:26:4422006-02-15 21:30:53
115211542005-05-31 21:42:092732592005-06-08 16:40:0912006-02-15 21:30:53
115311552005-05-31 22:17:1120482512005-06-04 20:27:1122006-02-15 21:30:53
115411562005-05-31 22:37:344601062005-06-01 23:02:3422006-02-15 21:30:53
115511572005-05-31 22:47:451449612005-06-02 18:01:4512006-02-15 21:30:53
\n", + "

1156 rows × 7 columns

\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", + "1151 1153 2005-05-31 21:36:44 2725 506 \n", + "1152 1154 2005-05-31 21:42:09 2732 59 \n", + "1153 1155 2005-05-31 22:17:11 2048 251 \n", + "1154 1156 2005-05-31 22:37:34 460 106 \n", + "1155 1157 2005-05-31 22:47:45 1449 61 \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 \n", + "... ... ... ... \n", + "1151 2005-06-10 01:26:44 2 2006-02-15 21:30:53 \n", + "1152 2005-06-08 16:40:09 1 2006-02-15 21:30:53 \n", + "1153 2005-06-04 20:27:11 2 2006-02-15 21:30:53 \n", + "1154 2005-06-01 23:02:34 2 2006-02-15 21:30:53 \n", + "1155 2005-06-02 18:01:45 1 2006-02-15 21:30:53 \n", + "\n", + "[1156 rows x 7 columns]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def rentals_month(eng,mth, yr):\n", + " with eng.connect() as connection:\n", + " # Getting how many loans were granted every year, and the month of each duration.\n", + " txt = f'select * from rental where month(rental_date) = {mth} and year(rental_date) = {yr};'\n", + " query = text(txt)\n", + " result = connection.execute(query)\n", + " df = pd.DataFrame(result.all())\n", + " return df" + ] + }, + { + "cell_type": "markdown", + "id": "7f13e61b-dd65-4a63-be2a-9e6c8f59ea7e", + "metadata": {}, + "source": [ + "Develop a Python function called rental_count_month that takes the DataFrame provided by rentals_month as input along \n", + "with the month and year and returns a new DataFrame containing the number of rentals made by each customer_id during the \n", + "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 \n", + "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\"." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "6cc2648a-4555-4295-a6bc-6eee534041c8", + "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_12_2005
01649
12320
231265
352958
461550
.........
5155942736
516595613
5175964265
518597548
5195991008
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_12_2005\n", + "0 1 649\n", + "1 2 320\n", + "2 3 1265\n", + "3 5 2958\n", + "4 6 1550\n", + ".. ... ...\n", + "515 594 2736\n", + "516 595 613\n", + "517 596 4265\n", + "518 597 548\n", + "519 599 1008\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def rental_count_month(dataframe,mth,yr):\n", + " results = dataframe.groupby('customer_id')['rental_id'].sum()\n", + " results = results.reset_index(name=f'rentals_{mth:02d}_{yr}')\n", + " return results" + ] + }, + { + "cell_type": "markdown", + "id": "94e78488-f8af-4ca2-b6a6-a547aa181ee4", + "metadata": {}, + "source": [ + "Create a Python function called compare_rentals that takes two DataFrames as input containing the number of \n", + "rentals made by each customer in different months and years. The function should return a combined DataFrame \n", + "with a new 'difference' column, which is the difference between the number of rentals in the two months." + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "831b6248-a764-482f-9ae5-1b7448696299", + "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_05_2005rentals_12_2005difference
0164913763-13114
123202128-1808
2312657811-6546
35295810892-7934
4615507727-6177
...............
507594273613667-10931
5085956134541-3928
50959642654411-146
5105975488276-7728
511599100812142-11134
\n", + "

512 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_12_2005 difference\n", + "0 1 649 13763 -13114\n", + "1 2 320 2128 -1808\n", + "2 3 1265 7811 -6546\n", + "3 5 2958 10892 -7934\n", + "4 6 1550 7727 -6177\n", + ".. ... ... ... ...\n", + "507 594 2736 13667 -10931\n", + "508 595 613 4541 -3928\n", + "509 596 4265 4411 -146\n", + "510 597 548 8276 -7728\n", + "511 599 1008 12142 -11134\n", + "\n", + "[512 rows x 4 columns]" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def compare_rentals(df1,df2):\n", + " df3 = df1.merge(df2)\n", + " df3['difference']= df3.iloc[:,-2]-df3.iloc[:,-1]\n", + " return df3" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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 +} From 8c65ba2a0a4b3ee77b7ad4cce8d5ebc01b17cbe0 Mon Sep 17 00:00:00 2001 From: claireley Date: Fri, 29 May 2026 16:21:49 +0200 Subject: [PATCH 2/2] solved lab --- SQL-Python-connection.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SQL-Python-connection.ipynb b/SQL-Python-connection.ipynb index fd3bf54..a66bf0b 100644 --- a/SQL-Python-connection.ipynb +++ b/SQL-Python-connection.ipynb @@ -387,7 +387,7 @@ ], "source": [ "def rental_count_month(dataframe,mth,yr):\n", - " results = dataframe.groupby('customer_id')['rental_id'].sum()\n", + " results = dataframe.groupby('customer_id')['rental_id'].count()\n", " results = results.reset_index(name=f'rentals_{mth:02d}_{yr}')\n", " return results" ]