diff --git a/Lab-Python-SQL-connection.ipynb b/Lab-Python-SQL-connection.ipynb new file mode 100644 index 0000000..9e12b14 --- /dev/null +++ b/Lab-Python-SQL-connection.ipynb @@ -0,0 +1,669 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "eea644bb-2369-442b-909b-d11629cb8f7a", + "metadata": {}, + "source": [ + "# LAB | Connecting Python to SQL" + ] + }, + { + "cell_type": "markdown", + "id": "8babf007-0a62-4c80-b363-c5c74aae5ed9", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "markdown", + "id": "acf66288-3855-48f7-a8bc-21eeddfd0f19", + "metadata": {}, + "source": [ + "### 1. Establish a connection between Python and the Sakila database." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "20e2c1bd-7d42-4e73-9f4a-5b554db975f2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting sqlalchemy\n", + " Downloading sqlalchemy-2.0.51-py3-none-any.whl.metadata (9.5 kB)\n", + "Collecting greenlet>=1 (from sqlalchemy)\n", + " Downloading greenlet-3.5.3-cp314-cp314-macosx_11_0_universal2.whl.metadata (3.8 kB)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in /Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages (from sqlalchemy) (4.15.0)\n", + "Downloading sqlalchemy-2.0.51-py3-none-any.whl (1.9 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.9/1.9 MB\u001b[0m \u001b[31m21.5 MB/s\u001b[0m \u001b[33m0:00:00\u001b[0m\n", + "\u001b[?25hDownloading greenlet-3.5.3-cp314-cp314-macosx_11_0_universal2.whl (288 kB)\n", + "Installing collected packages: greenlet, sqlalchemy\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2/2\u001b[0m [sqlalchemy]2\u001b[0m [sqlalchemy]\n", + "\u001b[1A\u001b[2KSuccessfully installed greenlet-3.5.3 sqlalchemy-2.0.51\n" + ] + } + ], + "source": [ + "!pip install sqlalchemy" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fbb51cb4-ace5-48ca-af83-f86134928f2c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting pymysql\n", + " Downloading pymysql-1.2.0-py3-none-any.whl.metadata (4.3 kB)\n", + "Downloading pymysql-1.2.0-py3-none-any.whl (45 kB)\n", + "Installing collected packages: pymysql\n", + "Successfully installed pymysql-1.2.0\n" + ] + } + ], + "source": [ + "!pip install pymysql" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "93f2b756-dfed-48df-8b5b-468f410e7115", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Version: 2.0.51\n", + "Location: /Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip show sqlalchemy | grep Version" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "86ec2466-1c24-41ba-84ba-525eac33b7ee", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + " ········\n" + ] + } + ], + "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()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c62a936c-67b8-416c-9c74-b7138f54fbbc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 7, + "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": 9, + "id": "2460dcd7-2229-40a7-a43f-07e8cb1f41f9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlalchemy import text\n", + "\n", + "with engine.connect() as connection:\n", + " query1 = text('SELECT * FROM staff')\n", + " result1 = connection.execute(query1)\n", + "\n", + "result1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40688963-52da-4826-95ff-afaaea7c9d78", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "1811c7dc-0599-45db-be66-a418668cabb7", + "metadata": {}, + "source": [ + "### 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." + ] + }, + { + "cell_type": "markdown", + "id": "2559637a-36eb-418f-92ac-662dac6e31a6", + "metadata": {}, + "source": [ + "The function should take in three parameters:\n", + "\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", + "\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, and return it as a pandas DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5959c1e7-5b3c-4011-90db-55620f1ab0db", + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import text\n", + "\n", + "def rentals_month(month, year):\n", + " query_rentals_month = text(\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " WHERE MONTH(rental_date) = :month\n", + " AND YEAR(rental_date) = :year\n", + " \"\"\")\n", + "\n", + " df_rentals_month = pd.read_sql(\n", + " query_rentals_month,\n", + " engine,\n", + " params={\n", + " \"month\": month,\n", + " \"year\": year\n", + " }\n", + " )\n", + "\n", + " return df_rentals_month" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c532230f-d095-458c-bb3f-26c4cde30725", + "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", + "
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": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_2005 = rentals_month(5, 2005)\n", + "\n", + "may_2005.head()" + ] + }, + { + "cell_type": "markdown", + "id": "e33068c1-b2c1-4c94-b742-f807e6f127dc", + "metadata": {}, + "source": [ + "### 3. Develop a Python function called rental_count_month that takes the DataFrame." + ] + }, + { + "cell_type": "markdown", + "id": "16dd128f-5317-4d34-8534-8739956b3d2c", + "metadata": {}, + "source": [ + "The function should take 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.\n", + "\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, if the input month is 05 and the year is 2005, the column name should be \"rentals_05_2005\".\n", + "\n", + "Hint: Consider making use of pandas groupby()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fc994e1-cb17-4440-b1e9-6500f408ec24", + "metadata": {}, + "outputs": [], + "source": [ + "#To count we can use:\n", + "df.groupby(\"customer_id\").size()\n", + "#which will count all the rows a customer_id is in\n", + "#This results in a series" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "158b70e6-e808-45c4-abab-1c5339635771", + "metadata": {}, + "outputs": [], + "source": [ + "#to convert it to a dataframe\n", + "df.groupby(\"customer_id\").size().reset_index()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "07218de7-4458-4b7e-94bd-47f462223155", + "metadata": {}, + "outputs": [], + "source": [ + "def rental_count_month(df, month, year):\n", + "\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": "code", + "execution_count": 16, + "id": "07171c90-b830-4b79-9105-f8f69ed86375", + "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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_2005 = rentals_month(5, 2005)\n", + "\n", + "rental_counts = rental_count_month(may_2005, 5, 2005)\n", + "\n", + "rental_counts.head()" + ] + }, + { + "cell_type": "markdown", + "id": "ff415886-a2d9-416e-adf5-fb5d9b2a8a40", + "metadata": {}, + "source": [ + "### 4. Create a Python function called compare_rentals that takes two DataFrames as input" + ] + }, + { + "cell_type": "markdown", + "id": "be89edb5-ad36-4ea0-8018-aa980f33ac36", + "metadata": {}, + "source": [ + "The function should take 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." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "9ba634c1-e56d-4c23-9b4c-5bb5c4c6e987", + "metadata": {}, + "outputs": [], + "source": [ + "may = rental_count_month(rentals_month(5, 2005), 5, 2005)\n", + "\n", + "june = rental_count_month(rentals_month(6, 2005), 6, 2005)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "4c2910bb-9028-407f-9ab7-c4fb632c7b10", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df_customer_time1, df_customer_time2):\n", + "\n", + " comparison_df = pd.merge(\n", + " df_customer_time1,\n", + " df_customer_time2,\n", + " on=\"customer_id\"\n", + " )\n", + "\n", + " #stage the columnsto add to the new df\n", + " #this extracts a list(column) from each of the input dataframes\n", + " col1 = df_customer_time1.columns[1]\n", + " col2 = df_customer_time2.columns[1]\n", + "\n", + " comparison_df[\"difference\"] = comparison_df[col2] - comparison_df[col1]\n", + "\n", + " return comparison_df" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "7e7854b3-97fa-48c0-ab57-c2e496899f85", + "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": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compare_rentals(may, june).head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0a4682cc-f923-4f38-a5d6-0c341ff2d042", + "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.14.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}