From 5e24b9e52fa08fd86ec809b7a1f632120e65fbfb Mon Sep 17 00:00:00 2001 From: Filipa-Amado Date: Sun, 26 Nov 2023 01:03:02 +0000 Subject: [PATCH 1/2] SQL_9 --- SQL_9_F.sql | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 SQL_9_F.sql diff --git a/SQL_9_F.sql b/SQL_9_F.sql new file mode 100644 index 0000000..5873b01 --- /dev/null +++ b/SQL_9_F.sql @@ -0,0 +1,67 @@ + +-- In this lab we will find the customers who were active in consecutive months of May and June. Follow the steps to complete the analysis. + +-- 1.Create a table `rentals_may` to store the data from rental table with information for the month of May. +create temporary table sakila.rental_may +select *, date_format(rental_date, "%M") as month +from sakila.rental +where date_format(rental_date, "%M") = 'May'; + +-- to make sure we only have the month of May +select distinct month +from sakila.rental_may; + +-- 2.Create a table `rentals_june` to store the data from rental table with information for the month of June. +create temporary table sakila.rental_june +select *, date_format(rental_date, "%M") as month +from sakila.rental +where date_format(rental_date, "%M") = 'June'; + +select distinct month from sakila.rental_june; + +-- 3.Check the number of rentals for each customer for May. +select * from sakila.rental_may; +select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer +from sakila.rental_may rm +left join sakila.customer c +on rm.customer_id = c.customer_id +group by rm.customer_id +order by count(rental_id) desc; + +-- 4. Check the number of rentals for each customer for June. +select * from sakila.rental_june; +select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer +from sakila.rental_june rm +left join sakila.customer c +on rm.customer_id = c.customer_id +group by rm.customer_id +order by count(rental_id) desc; + + +-- 5. Create a Python connection with SQL database and retrieve the results of the last two queries (also mentioned below) as dataframes: + -- a.Check the number of rentals for each customer for May +create temporary table sakila.customer_rentals_may +select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer +from sakila.rental_may rm +left join sakila.customer c +on rm.customer_id = c.customer_id +group by rm.customer_id +order by count(rental_id) desc; + +select* from sakila.customer_rentals_may; + + -- b.Check the number of rentals for each customer for June +create temporary table sakila.customer_rentals_june +select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer +from sakila.rental_june rm +left join sakila.customer c +on rm.customer_id = c.customer_id +group by rm.customer_id +order by count(rental_id) desc; + +select* from sakila.customer_rentals_june; + + +-- 6.Write a function that checks if customer borrowed more or less films in the month of June as compared to May. + -- **Hint**: For this part, you can create a join between the two dataframes created before, using the merge function available for pandas dataframes. + -- Here is a link to the documentation for the [merge function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html). \ No newline at end of file From 1c1c74ac52fa39157a74d21f9128aec24aa9957b Mon Sep 17 00:00:00 2001 From: Filipa-Amado Date: Sat, 30 Dec 2023 23:04:02 +0000 Subject: [PATCH 2/2] [lab_SQL_9]FilipaA --- .../SQL_9_python-checkpoint.ipynb | 660 ++++++++++++++++++ SQL_9_F.sql | 7 +- SQL_9_python.ipynb | 660 ++++++++++++++++++ 3 files changed, 1325 insertions(+), 2 deletions(-) create mode 100644 .ipynb_checkpoints/SQL_9_python-checkpoint.ipynb create mode 100644 SQL_9_python.ipynb diff --git a/.ipynb_checkpoints/SQL_9_python-checkpoint.ipynb b/.ipynb_checkpoints/SQL_9_python-checkpoint.ipynb new file mode 100644 index 0000000..b246213 --- /dev/null +++ b/.ipynb_checkpoints/SQL_9_python-checkpoint.ipynb @@ -0,0 +1,660 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "0cac804b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: mysql.connector in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (2.2.9)\n", + "Requirement already satisfied: mysql.connector.python in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (8.2.0)\n", + "Requirement already satisfied: protobuf<=4.21.12,>=4.21.1 in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (from mysql.connector.python) (4.21.12)\n", + "Requirement already satisfied: sqlalchemy in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (1.4.22)\n", + "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (from sqlalchemy) (1.1.1)\n", + "Requirement already satisfied: pymysql in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (1.1.0)\n" + ] + } + ], + "source": [ + "\n", + "!pip install mysql.connector\n", + "!pip install mysql.connector.python\n", + "!pip install sqlalchemy\n", + "!pip install pymysql\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "a78c4743", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "89e210be", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "········\n" + ] + } + ], + "source": [ + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2d9e5c65", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/sakila'\n", + "engine = create_engine(connection_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9c8a66a8", + "metadata": { + "scrolled": true + }, + "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_full_nameJune_rentals
0BRENDA WRIGHT11
1ALEX GRESHAM10
2FRANK WAGGONER9
3DAISY BATES9
4IAN STILL9
.........
585FRANCIS SIKES1
586ALLEN BUTTERFIELD1
587GREGORY MAULDIN1
588LAURA RODRIGUEZ1
589ELLEN SIMPSON1
\n", + "

590 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name June_rentals\n", + "0 BRENDA WRIGHT 11\n", + "1 ALEX GRESHAM 10\n", + "2 FRANK WAGGONER 9\n", + "3 DAISY BATES 9\n", + "4 IAN STILL 9\n", + ".. ... ...\n", + "585 FRANCIS SIKES 1\n", + "586 ALLEN BUTTERFIELD 1\n", + "587 GREGORY MAULDIN 1\n", + "588 LAURA RODRIGUEZ 1\n", + "589 ELLEN SIMPSON 1\n", + "\n", + "[590 rows x 2 columns]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_june = pd.read_sql_query('select * from sakila.customer_rentals_june', engine)\n", + "rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1ad03830", + "metadata": { + "scrolled": true + }, + "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_full_nameMay_rentals
0SUE PETERS8
1LESLIE SEWARD7
2EDNA WEST7
3CASSANDRA WALTERS6
4MINNIE ROMERO6
.........
515JOEL FRANCISCO1
516JACK FOUST1
517DOROTHY TAYLOR1
518ANITA MORALES1
519KATHERINE RIVERA1
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals\n", + "0 SUE PETERS 8\n", + "1 LESLIE SEWARD 7\n", + "2 EDNA WEST 7\n", + "3 CASSANDRA WALTERS 6\n", + "4 MINNIE ROMERO 6\n", + ".. ... ...\n", + "515 JOEL FRANCISCO 1\n", + "516 JACK FOUST 1\n", + "517 DOROTHY TAYLOR 1\n", + "518 ANITA MORALES 1\n", + "519 KATHERINE RIVERA 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_may = pd.read_sql_query('select * from sakila.customer_rentals_may', engine)\n", + "rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d18008e9", + "metadata": { + "scrolled": true + }, + "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", + "
customer_full_nameMay_rentalsJune_rentals
0SUE PETERS88
1LESLIE SEWARD75
2EDNA WEST75
3CASSANDRA WALTERS63
4MINNIE ROMERO65
............
507CLAUDIA FULLER18
508JOEL FRANCISCO17
509JACK FOUST13
510DOROTHY TAYLOR15
511ANITA MORALES11
\n", + "

512 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals June_rentals\n", + "0 SUE PETERS 8 8\n", + "1 LESLIE SEWARD 7 5\n", + "2 EDNA WEST 7 5\n", + "3 CASSANDRA WALTERS 6 3\n", + "4 MINNIE ROMERO 6 5\n", + ".. ... ... ...\n", + "507 CLAUDIA FULLER 1 8\n", + "508 JOEL FRANCISCO 1 7\n", + "509 JACK FOUST 1 3\n", + "510 DOROTHY TAYLOR 1 5\n", + "511 ANITA MORALES 1 1\n", + "\n", + "[512 rows x 3 columns]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_may_june = pd.merge(rentals_may, rentals_june, on='customer_full_name')\n", + "rentals_may_june\n" + ] + }, + { + "cell_type": "markdown", + "id": "b3a6f3d0", + "metadata": {}, + "source": [ + "-- 6.Write a function that checks if customer borrowed more or less films in the month of June as compared to May." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "96323349", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df, column1, column2):\n", + " result = []\n", + "\n", + " for index, row in df.iterrows():\n", + " if row[column1] > row[column2]:\n", + " result.append(f\"{column1} is higher\")\n", + " elif row[column1] < row[column2]:\n", + " result.append(f\"{column2} is higher\")\n", + " else:\n", + " result.append(\"Rentals in May and June are the same\")\n", + "\n", + " df['rentals_dif'] = result\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "fc4f7a94", + "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_full_nameMay_rentalsJune_rentalsrentals_dif
0SUE PETERS88Rentals in May and June are the same
1LESLIE SEWARD75May_rentals is higher
2EDNA WEST75May_rentals is higher
3CASSANDRA WALTERS63May_rentals is higher
4MINNIE ROMERO65May_rentals is higher
...............
507CLAUDIA FULLER18June_rentals is higher
508JOEL FRANCISCO17June_rentals is higher
509JACK FOUST13June_rentals is higher
510DOROTHY TAYLOR15June_rentals is higher
511ANITA MORALES11Rentals in May and June are the same
\n", + "

512 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals June_rentals \\\n", + "0 SUE PETERS 8 8 \n", + "1 LESLIE SEWARD 7 5 \n", + "2 EDNA WEST 7 5 \n", + "3 CASSANDRA WALTERS 6 3 \n", + "4 MINNIE ROMERO 6 5 \n", + ".. ... ... ... \n", + "507 CLAUDIA FULLER 1 8 \n", + "508 JOEL FRANCISCO 1 7 \n", + "509 JACK FOUST 1 3 \n", + "510 DOROTHY TAYLOR 1 5 \n", + "511 ANITA MORALES 1 1 \n", + "\n", + " rentals_dif \n", + "0 Rentals in May and June are the same \n", + "1 May_rentals is higher \n", + "2 May_rentals is higher \n", + "3 May_rentals is higher \n", + "4 May_rentals is higher \n", + ".. ... \n", + "507 June_rentals is higher \n", + "508 June_rentals is higher \n", + "509 June_rentals is higher \n", + "510 June_rentals is higher \n", + "511 Rentals in May and June are the same \n", + "\n", + "[512 rows x 4 columns]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compare_rentals (rentals_may_june, 'May_rentals', 'June_rentals')" + ] + } + ], + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/SQL_9_F.sql b/SQL_9_F.sql index 5873b01..931534a 100644 --- a/SQL_9_F.sql +++ b/SQL_9_F.sql @@ -7,6 +7,7 @@ select *, date_format(rental_date, "%M") as month from sakila.rental where date_format(rental_date, "%M") = 'May'; + -- to make sure we only have the month of May select distinct month from sakila.rental_may; @@ -40,7 +41,7 @@ order by count(rental_id) desc; -- 5. Create a Python connection with SQL database and retrieve the results of the last two queries (also mentioned below) as dataframes: -- a.Check the number of rentals for each customer for May -create temporary table sakila.customer_rentals_may +create table sakila.customer_rentals_may select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer from sakila.rental_may rm left join sakila.customer c @@ -51,7 +52,7 @@ order by count(rental_id) desc; select* from sakila.customer_rentals_may; -- b.Check the number of rentals for each customer for June -create temporary table sakila.customer_rentals_june +create table sakila.customer_rentals_june select concat(first_name, ' ', last_name) customer_full_name, count(rental_id) as rental_per_costumer from sakila.rental_june rm left join sakila.customer c @@ -60,6 +61,8 @@ group by rm.customer_id order by count(rental_id) desc; select* from sakila.customer_rentals_june; +select * from sakila.actor; + -- 6.Write a function that checks if customer borrowed more or less films in the month of June as compared to May. diff --git a/SQL_9_python.ipynb b/SQL_9_python.ipynb new file mode 100644 index 0000000..b246213 --- /dev/null +++ b/SQL_9_python.ipynb @@ -0,0 +1,660 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "0cac804b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: mysql.connector in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (2.2.9)\n", + "Requirement already satisfied: mysql.connector.python in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (8.2.0)\n", + "Requirement already satisfied: protobuf<=4.21.12,>=4.21.1 in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (from mysql.connector.python) (4.21.12)\n", + "Requirement already satisfied: sqlalchemy in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (1.4.22)\n", + "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (from sqlalchemy) (1.1.1)\n", + "Requirement already satisfied: pymysql in c:\\users\\pedro gomes\\anaconda3\\lib\\site-packages (1.1.0)\n" + ] + } + ], + "source": [ + "\n", + "!pip install mysql.connector\n", + "!pip install mysql.connector.python\n", + "!pip install sqlalchemy\n", + "!pip install pymysql\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "a78c4743", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "89e210be", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "········\n" + ] + } + ], + "source": [ + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2d9e5c65", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/sakila'\n", + "engine = create_engine(connection_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9c8a66a8", + "metadata": { + "scrolled": true + }, + "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_full_nameJune_rentals
0BRENDA WRIGHT11
1ALEX GRESHAM10
2FRANK WAGGONER9
3DAISY BATES9
4IAN STILL9
.........
585FRANCIS SIKES1
586ALLEN BUTTERFIELD1
587GREGORY MAULDIN1
588LAURA RODRIGUEZ1
589ELLEN SIMPSON1
\n", + "

590 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name June_rentals\n", + "0 BRENDA WRIGHT 11\n", + "1 ALEX GRESHAM 10\n", + "2 FRANK WAGGONER 9\n", + "3 DAISY BATES 9\n", + "4 IAN STILL 9\n", + ".. ... ...\n", + "585 FRANCIS SIKES 1\n", + "586 ALLEN BUTTERFIELD 1\n", + "587 GREGORY MAULDIN 1\n", + "588 LAURA RODRIGUEZ 1\n", + "589 ELLEN SIMPSON 1\n", + "\n", + "[590 rows x 2 columns]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_june = pd.read_sql_query('select * from sakila.customer_rentals_june', engine)\n", + "rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1ad03830", + "metadata": { + "scrolled": true + }, + "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_full_nameMay_rentals
0SUE PETERS8
1LESLIE SEWARD7
2EDNA WEST7
3CASSANDRA WALTERS6
4MINNIE ROMERO6
.........
515JOEL FRANCISCO1
516JACK FOUST1
517DOROTHY TAYLOR1
518ANITA MORALES1
519KATHERINE RIVERA1
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals\n", + "0 SUE PETERS 8\n", + "1 LESLIE SEWARD 7\n", + "2 EDNA WEST 7\n", + "3 CASSANDRA WALTERS 6\n", + "4 MINNIE ROMERO 6\n", + ".. ... ...\n", + "515 JOEL FRANCISCO 1\n", + "516 JACK FOUST 1\n", + "517 DOROTHY TAYLOR 1\n", + "518 ANITA MORALES 1\n", + "519 KATHERINE RIVERA 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_may = pd.read_sql_query('select * from sakila.customer_rentals_may', engine)\n", + "rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d18008e9", + "metadata": { + "scrolled": true + }, + "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", + "
customer_full_nameMay_rentalsJune_rentals
0SUE PETERS88
1LESLIE SEWARD75
2EDNA WEST75
3CASSANDRA WALTERS63
4MINNIE ROMERO65
............
507CLAUDIA FULLER18
508JOEL FRANCISCO17
509JACK FOUST13
510DOROTHY TAYLOR15
511ANITA MORALES11
\n", + "

512 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals June_rentals\n", + "0 SUE PETERS 8 8\n", + "1 LESLIE SEWARD 7 5\n", + "2 EDNA WEST 7 5\n", + "3 CASSANDRA WALTERS 6 3\n", + "4 MINNIE ROMERO 6 5\n", + ".. ... ... ...\n", + "507 CLAUDIA FULLER 1 8\n", + "508 JOEL FRANCISCO 1 7\n", + "509 JACK FOUST 1 3\n", + "510 DOROTHY TAYLOR 1 5\n", + "511 ANITA MORALES 1 1\n", + "\n", + "[512 rows x 3 columns]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rentals_may_june = pd.merge(rentals_may, rentals_june, on='customer_full_name')\n", + "rentals_may_june\n" + ] + }, + { + "cell_type": "markdown", + "id": "b3a6f3d0", + "metadata": {}, + "source": [ + "-- 6.Write a function that checks if customer borrowed more or less films in the month of June as compared to May." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "96323349", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df, column1, column2):\n", + " result = []\n", + "\n", + " for index, row in df.iterrows():\n", + " if row[column1] > row[column2]:\n", + " result.append(f\"{column1} is higher\")\n", + " elif row[column1] < row[column2]:\n", + " result.append(f\"{column2} is higher\")\n", + " else:\n", + " result.append(\"Rentals in May and June are the same\")\n", + "\n", + " df['rentals_dif'] = result\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "fc4f7a94", + "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_full_nameMay_rentalsJune_rentalsrentals_dif
0SUE PETERS88Rentals in May and June are the same
1LESLIE SEWARD75May_rentals is higher
2EDNA WEST75May_rentals is higher
3CASSANDRA WALTERS63May_rentals is higher
4MINNIE ROMERO65May_rentals is higher
...............
507CLAUDIA FULLER18June_rentals is higher
508JOEL FRANCISCO17June_rentals is higher
509JACK FOUST13June_rentals is higher
510DOROTHY TAYLOR15June_rentals is higher
511ANITA MORALES11Rentals in May and June are the same
\n", + "

512 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_full_name May_rentals June_rentals \\\n", + "0 SUE PETERS 8 8 \n", + "1 LESLIE SEWARD 7 5 \n", + "2 EDNA WEST 7 5 \n", + "3 CASSANDRA WALTERS 6 3 \n", + "4 MINNIE ROMERO 6 5 \n", + ".. ... ... ... \n", + "507 CLAUDIA FULLER 1 8 \n", + "508 JOEL FRANCISCO 1 7 \n", + "509 JACK FOUST 1 3 \n", + "510 DOROTHY TAYLOR 1 5 \n", + "511 ANITA MORALES 1 1 \n", + "\n", + " rentals_dif \n", + "0 Rentals in May and June are the same \n", + "1 May_rentals is higher \n", + "2 May_rentals is higher \n", + "3 May_rentals is higher \n", + "4 May_rentals is higher \n", + ".. ... \n", + "507 June_rentals is higher \n", + "508 June_rentals is higher \n", + "509 June_rentals is higher \n", + "510 June_rentals is higher \n", + "511 Rentals in May and June are the same \n", + "\n", + "[512 rows x 4 columns]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compare_rentals (rentals_may_june, 'May_rentals', 'June_rentals')" + ] + } + ], + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}