From 17c1317b3c91715033b309e96fe9e5404fb99f61 Mon Sep 17 00:00:00 2001 From: Mandana Soltani Date: Tue, 19 May 2026 20:45:38 +0200 Subject: [PATCH 1/2] lab submission --- README.md | 92 ------------------------------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index b0ef389..0000000 --- a/README.md +++ /dev/null @@ -1,92 +0,0 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) - -# LAB | Connecting Python to SQL - -
- -

Learning Goals

-
- - This lab allows you to practice and apply the concepts and techniques taught in class. - - Upon completion of this lab, you will be able to: - -- Write a Python script to connect to a relational database using the appropriate Python library and query it using SQL commands. - -
-
- -
- -
- -

Prerequisites

-
- -Before this starting this lab, you should have learnt about: - -- Basic SQL queries -- Python and Pandas -- Data Wrangling, which involves tasks such as grouping, aggregating, dealing with indexes, renaming columns, merging data and performing calculations. - -
-
- -
- - -## Introduction - -Welcome to the Connecting Python to SQL lab! - -In this lab, you will be working with the [Sakila](https://dev.mysql.com/doc/sakila/en/) database on movie rentals. Specifically, you will be practicing how to do basic SQL queries using Python. By connecting Python to SQL, you can leverage the power of both languages to efficiently manipulate and analyze large datasets. Throughout this lab, you will practice how to use Python to retrieve and manipulate data stored in the Sakila database using SQL queries. Let's get started! - -## Challenge - -In this lab, the objective is to identify the customers who were active in both May and June, and how did their activity differ between months. To achieve this, follow these steps: - -1. Establish a connection between Python and the Sakila database. - -2. 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: - - - `engine`: an object representing the database connection engine to be used to establish a connection to the Sakila database. - - `month`: an integer representing the month for which rental data is to be retrieved. - - `year`: an integer representing the year for which rental data is to be retrieved. - - 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. - -3. 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. - - 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". - - - *Hint: Consider making use of pandas [groupby()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)* - -4. 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. -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. - -## Requirements - -- Fork this repo -- Clone it to your machine - - - -## Getting Started - -Complete the challenges. Follow the instructions and add your code and explanations as necessary. - -## Submission - -- Upon completion, run the following commands: - -```bash -git add . -git commit -m "Solved lab" -git push origin master -``` - -- Paste the link of your lab in Student Portal. - - - From 2bea32dec7dd97bbad879d9101ea71850647e8fc Mon Sep 17 00:00:00 2001 From: Mandana Soltani Date: Tue, 19 May 2026 20:50:57 +0200 Subject: [PATCH 2/2] Add files via upload --- lab-sql-python-connection.ipynb | 1905 +++++++++++++++++++++++++++++++ 1 file changed, 1905 insertions(+) create mode 100644 lab-sql-python-connection.ipynb diff --git a/lab-sql-python-connection.ipynb b/lab-sql-python-connection.ipynb new file mode 100644 index 0000000..68e90fd --- /dev/null +++ b/lab-sql-python-connection.ipynb @@ -0,0 +1,1905 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "5a68bf75-3912-4bc3-8404-0a0448fc0d99", + "metadata": {}, + "outputs": [], + "source": [ + "###STEP 1 " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "91aa457a-ede9-460c-8e44-68e9a2cfec68", + "metadata": {}, + "outputs": [], + "source": [ + "#1\n", + "import mysql.connector\n", + "import pandas as pd\n", + "from getpass import getpass" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cf60a89c-267b-45fa-8fe9-e8bc0eeb827b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter your MySQL password: ········\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection successful!\n" + ] + } + ], + "source": [ + "#2\n", + "password = getpass(\"Enter your MySQL password: \")\n", + "\n", + "connection = mysql.connector.connect(\n", + " host='127.0.0.1',\n", + " port=3306,\n", + " user='root',\n", + " password=password,\n", + " database='sakila'\n", + ")\n", + "\n", + "cursor = connection.cursor()\n", + "\n", + "print(\"Connection successful!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "aff22e60-487a-4d84-9490-5d305ec7f3a8", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\manda\\AppData\\Local\\Temp\\ipykernel_27804\\4283134436.py:2: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " pd.read_sql(\"SHOW TABLES;\", connection)\n" + ] + }, + { + "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", + "
Tables_in_sakila
0actor
1actor_info
2address
3category
4city
5country
6customer
7customer_list
8customer_rental_summary
9film
10film_actor
11film_category
12film_list
13film_text
14inventory
15language
16nicer_but_slower_film_list
17payment
18rental
19sales_by_film_category
20sales_by_store
21staff
22staff_list
23store
\n", + "
" + ], + "text/plain": [ + " Tables_in_sakila\n", + "0 actor\n", + "1 actor_info\n", + "2 address\n", + "3 category\n", + "4 city\n", + "5 country\n", + "6 customer\n", + "7 customer_list\n", + "8 customer_rental_summary\n", + "9 film\n", + "10 film_actor\n", + "11 film_category\n", + "12 film_list\n", + "13 film_text\n", + "14 inventory\n", + "15 language\n", + "16 nicer_but_slower_film_list\n", + "17 payment\n", + "18 rental\n", + "19 sales_by_film_category\n", + "20 sales_by_store\n", + "21 staff\n", + "22 staff_list\n", + "23 store" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3 \n", + "pd.read_sql(\"SHOW TABLES;\", connection)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "bfffc12f-c952-4df1-bad2-89d1011123bb", + "metadata": {}, + "outputs": [], + "source": [ + "###STEP 2" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c8c82c37-052e-47bb-b7e3-dd41be7747c2", + "metadata": {}, + "outputs": [], + "source": [ + "#1 Function to retrieve rentals for a specific month and year\n", + "\n", + "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", + " rentals_df = pd.read_sql(query, engine)\n", + " return rentals_df" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "78fb9654-451e-4e3b-a344-a72a0b10a709", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\manda\\AppData\\Local\\Temp\\ipykernel_27804\\1474216370.py:10: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " rentals_df = pd.read_sql(query, engine)\n" + ] + }, + { + "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": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#2 Retrieve May 2005 rentals\n", + "may_rentals = rentals_month(connection, 5, 2005)\n", + "may_rentals.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "2cab297c-1f86-4abb-aeee-4f82843b6d9b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\manda\\AppData\\Local\\Temp\\ipykernel_27804\\1474216370.py:10: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " rentals_df = pd.read_sql(query, engine)\n" + ] + }, + { + "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
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
\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", + " 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 " + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3 Retrieve June 2005 rentals\n", + "june_rentals = rentals_month(connection, 6, 2005)\n", + "june_rentals.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "fe7874e1-ec44-41e2-9687-aea7ff4b05fd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "May rentals: (1156, 7)\n", + "June rentals: (2311, 7)\n" + ] + } + ], + "source": [ + "#4 Check the shape of both DataFrames\n", + "print(\"May rentals:\", may_rentals.shape)\n", + "print(\"June rentals:\", june_rentals.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "0ceb15e6-4efd-49f9-bbc4-ff771b49e2c2", + "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_idmay_rentals
012
121
232
353
463
\n", + "
" + ], + "text/plain": [ + " customer_id may_rentals\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": [ + "#5 Count rentals per customer in May\n", + "may_customer_activity = (\n", + " may_rentals\n", + " .groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=\"may_rentals\")\n", + ")\n", + "may_customer_activity.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "8687b6d3-19c7-4926-8eb1-f65ba1ac945c", + "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_idjune_rentals
017
121
234
346
455
\n", + "
" + ], + "text/plain": [ + " customer_id june_rentals\n", + "0 1 7\n", + "1 2 1\n", + "2 3 4\n", + "3 4 6\n", + "4 5 5" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#6 Count rentals per customer in June\n", + "\n", + "june_customer_activity = (\n", + " june_rentals\n", + " .groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=\"june_rentals\")\n", + ")\n", + "june_customer_activity.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "a5ef88d3-a63e-4ea3-84e8-b6b1092c60f2", + "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", + "
customer_idmay_rentalsjune_rentals
0127
1211
2324
3535
4634
\n", + "
" + ], + "text/plain": [ + " customer_id may_rentals june_rentals\n", + "0 1 2 7\n", + "1 2 1 1\n", + "2 3 2 4\n", + "3 5 3 5\n", + "4 6 3 4" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#7 Find customers active in both May and June\n", + "\n", + "active_both_months = pd.merge(\n", + " may_customer_activity,\n", + " june_customer_activity,\n", + " on=\"customer_id\",\n", + " how=\"inner\"\n", + ")\n", + "active_both_months.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f54a9815-8c38-4d66-ad33-824c479c4697", + "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_idmay_rentalsjune_rentalsdifference
01275
12110
23242
35352
46341
\n", + "
" + ], + "text/plain": [ + " customer_id may_rentals june_rentals 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" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#8 Compare customer activity between June and May\n", + "\n", + "active_both_months[\"difference\"] = (\n", + " active_both_months[\"june_rentals\"] \n", + " - active_both_months[\"may_rentals\"]\n", + ")\n", + "\n", + "active_both_months.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "239afbe1-0784-4f21-ad96-40b5c7f2daf9", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\manda\\AppData\\Local\\Temp\\ipykernel_27804\\1945799371.py:3: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n", + " customer_df = pd.read_sql(\n" + ] + }, + { + "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", + "
customer_idmay_rentalsjune_rentalsdifferencefirst_namelast_name
01275MARYSMITH
12110PATRICIAJOHNSON
23242LINDAWILLIAMS
35352ELIZABETHBROWN
46341JENNIFERDAVIS
\n", + "
" + ], + "text/plain": [ + " customer_id may_rentals june_rentals difference first_name last_name\n", + "0 1 2 7 5 MARY SMITH\n", + "1 2 1 1 0 PATRICIA JOHNSON\n", + "2 3 2 4 2 LINDA WILLIAMS\n", + "3 5 3 5 2 ELIZABETH BROWN\n", + "4 6 3 4 1 JENNIFER DAVIS" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#9 Add customer names\n", + "\n", + "customer_df = pd.read_sql(\n", + " \"\"\"\n", + " SELECT \n", + " customer_id,\n", + " first_name,\n", + " last_name\n", + " FROM customer;\n", + " \"\"\",\n", + " connection\n", + ")\n", + "\n", + "final_df = pd.merge(\n", + " active_both_months,\n", + " customer_df,\n", + " on=\"customer_id\",\n", + " how=\"left\"\n", + ")\n", + "\n", + "final_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "9bd72030-7668-4740-ab48-11fc05a4141b", + "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", + "
customer_idfirst_namelast_namemay_rentalsjune_rentalsdifference
01MARYSMITH275
12PATRICIAJOHNSON110
23LINDAWILLIAMS242
35ELIZABETHBROWN352
46JENNIFERDAVIS341
\n", + "
" + ], + "text/plain": [ + " customer_id first_name last_name may_rentals june_rentals difference\n", + "0 1 MARY SMITH 2 7 5\n", + "1 2 PATRICIA JOHNSON 1 1 0\n", + "2 3 LINDA WILLIAMS 2 4 2\n", + "3 5 ELIZABETH BROWN 3 5 2\n", + "4 6 JENNIFER DAVIS 3 4 1" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#10 Reorder columns\n", + "\n", + "final_df = final_df[\n", + " [\n", + " \"customer_id\",\n", + " \"first_name\",\n", + " \"last_name\",\n", + " \"may_rentals\",\n", + " \"june_rentals\",\n", + " \"difference\"\n", + " ]\n", + "]\n", + "\n", + "final_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "d83ad443-381d-4b3e-9b20-fc99f418aaa7", + "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", + "
customer_idfirst_namelast_namemay_rentalsjune_rentalsdifference
386454ALEXGRESHAM1109
178213GINAWILLIAMSON198
248295DAISYBATES198
389457BILLGAVIN198
322380RUSSELLBRINSON187
194234CLAUDIAFULLER187
218260CHRISTYVARGAS187
481561IANSTILL297
2327SHIRLEYALLEN187
224267MARGIEWADE396
\n", + "
" + ], + "text/plain": [ + " customer_id first_name last_name may_rentals june_rentals difference\n", + "386 454 ALEX GRESHAM 1 10 9\n", + "178 213 GINA WILLIAMSON 1 9 8\n", + "248 295 DAISY BATES 1 9 8\n", + "389 457 BILL GAVIN 1 9 8\n", + "322 380 RUSSELL BRINSON 1 8 7\n", + "194 234 CLAUDIA FULLER 1 8 7\n", + "218 260 CHRISTY VARGAS 1 8 7\n", + "481 561 IAN STILL 2 9 7\n", + "23 27 SHIRLEY ALLEN 1 8 7\n", + "224 267 MARGIE WADE 3 9 6" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 11 Sort by biggest increase in activity\n", + "final_df.sort_values(by=\"difference\", ascending=False).head(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b920ac0b-30a9-4303-b80c-dd4c3303497b", + "metadata": {}, + "outputs": [], + "source": [ + "###STEP 3" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "d1365b11-f43a-4c71-acba-6d292fb96269", + "metadata": {}, + "outputs": [], + "source": [ + "# 1 Function: rental_count_month\n", + "\n", + "def rental_count_month(df, month, year):\n", + " \n", + " # Create dynamic column name\n", + " column_name = f\"rentals_{month:02d}_{year}\"\n", + " \n", + " # Count rentals by customer_id\n", + " rental_count_df = (\n", + " df.groupby(\"customer_id\")\n", + " .size()\n", + " .reset_index(name=column_name)\n", + " )\n", + " \n", + " return rental_count_df" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "33b3f0e1-3625-4a46-8906-399c63f00683", + "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": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#2 function for May 2005\n", + "may_rental_counts = rental_count_month(\n", + " may_rentals,\n", + " 5,\n", + " 2005\n", + ")\n", + "may_rental_counts.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b2ff114f-e961-438c-a203-5161297d349b", + "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_06_2005
017
121
234
346
455
\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" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#3 function for June 2005\n", + "june_rental_counts = rental_count_month(\n", + " june_rentals,\n", + " 6,\n", + " 2005\n", + ")\n", + "june_rental_counts.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "85e80cf0-b588-43c4-8fbd-9a8304f500dc", + "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": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#4\n", + "may_rental_counts = rental_count_month(may_rentals, 5, 2005)\n", + "may_rental_counts.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "f1b58354-0285-4360-890d-d6bd308103fc", + "metadata": {}, + "outputs": [], + "source": [ + "### STEP 4" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "3ee23b04-f683-4c24-b1a8-691da8c0ece3", + "metadata": {}, + "outputs": [], + "source": [ + "#1 Function compare_rentals\n", + "\n", + "def compare_rentals(df1, df2):\n", + " \n", + "# Merge both DataFrames using customer_id\n", + " comparison_df = pd.merge(\n", + " df1,\n", + " df2,\n", + " on=\"customer_id\",\n", + " how=\"inner\"\n", + " )\n", + " \n", + "# Get rental column names automatically\n", + " rental_col_1 = comparison_df.columns[1]\n", + " rental_col_2 = comparison_df.columns[2]\n", + " \n", + "# Create difference column\n", + " comparison_df[\"difference\"] = (\n", + " comparison_df[rental_col_2]\n", + " - comparison_df[rental_col_1]\n", + " )\n", + " return comparison_df" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "972eeafd-0fb5-40ec-884a-c338973461bb", + "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_2005difference
01275
12110
23242
35352
46341
\n", + "
" + ], + "text/plain": [ + " 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" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#2 Compare May and June rentals\n", + "\n", + "comparison = compare_rentals(\n", + " may_rental_counts,\n", + " june_rental_counts\n", + ")\n", + "comparison.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "a59d3248-8f0c-4bb9-b3ad-f1cd2bbb3550", + "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_06_2005difference
3864541109
178213198
248295198
389457198
322380187
...............
111451-4
50959662-4
21025051-4
13616162-4
17320761-5
\n", + "

512 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_06_2005 difference\n", + "386 454 1 10 9\n", + "178 213 1 9 8\n", + "248 295 1 9 8\n", + "389 457 1 9 8\n", + "322 380 1 8 7\n", + ".. ... ... ... ...\n", + "11 14 5 1 -4\n", + "509 596 6 2 -4\n", + "210 250 5 1 -4\n", + "136 161 6 2 -4\n", + "173 207 6 1 -5\n", + "\n", + "[512 rows x 4 columns]" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#3\n", + "comparison.sort_values(by=\"difference\", ascending=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc44a35f-3cef-47f8-9d6b-0f08e0a30d64", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}