diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf73cf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.ipynb_checkpoints/ +__pycache__/ +*.pyc diff --git a/lab_sql_python_connection.ipynb b/lab_sql_python_connection.ipynb new file mode 100644 index 0000000..d9c80f7 --- /dev/null +++ b/lab_sql_python_connection.ipynb @@ -0,0 +1,677 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4e28bb67-7d08-42f8-a78f-9519b29cf2c3", + "metadata": {}, + "source": [ + "# Use password and user from SQL" + ] + }, + { + "cell_type": "markdown", + "id": "363a6f5b-6923-4572-bd12-bd2078280117", + "metadata": {}, + "source": [ + "## Establish a connection between Python and the Sakila database." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b0a1e764-7cad-4676-a286-c89fb7946a52", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import getpass" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "522f936d-cdce-4523-bbf5-ed5ffd1983de", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + " ········\n" + ] + } + ], + "source": [ + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "5a526192-f227-4def-8157-90e2528d6923", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Engine(mysql+pymysql://root:***@localhost/sakila)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bd = \"sakila\"\n", + "\n", + "connection_string = (\n", + " 'mysql+pymysql://root:' \n", + " + password \n", + " + '@localhost/' \n", + " + bd\n", + ")\n", + "\n", + "engine = create_engine(connection_string)\n", + "\n", + "engine" + ] + }, + { + "cell_type": "markdown", + "id": "15d7a164-f191-4db0-98d0-a5a2bddff82b", + "metadata": {}, + "source": [ + "### Double checking if it worked" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "202c7672-c08a-4926-b8e6-ff6b57e25318", + "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": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_test = pd.read_sql(\"SELECT * FROM rental LIMIT 5;\", engine)\n", + "\n", + "df_test" + ] + }, + { + "cell_type": "markdown", + "id": "4313dc91-7d4e-4ff8-ae02-104884830b22", + "metadata": {}, + "source": [ + "## Write a Python function called rentals_month " + ] + }, + { + "cell_type": "markdown", + "id": "5b298808-ed53-43c2-8dad-e4ce8d46d5a8", + "metadata": {}, + "source": [ + "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:\n", + "\n", + "- engine: an object representing the database connection engine to be used to establish a connection to the Sakila database.\n", + "\n", + "- month: an integer representing the month for which rental data is to be retrieved.\n", + "\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": 5, + "id": "218d9cf8-1111-4218-9b74-0601694c4dc0", + "metadata": {}, + "outputs": [], + "source": [ + "def rentals_month(engine, month, year):\n", + "\n", + " query = f\"\"\"\n", + " SELECT *\n", + " FROM rental\n", + " WHERE MONTH(rental_date) = {month}\n", + " AND YEAR(rental_date) = {year}\n", + " \"\"\"\n", + "\n", + " return pd.read_sql(query, engine)" + ] + }, + { + "cell_type": "markdown", + "id": "767598e9-51ad-4f2b-9214-c60bbf646c43", + "metadata": {}, + "source": [ + "### Test example with may 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "7c859a02-6b47-4b59-8750-b029f9d69493", + "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": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may = rentals_month(engine, 5, 2005)\n", + "may.head()" + ] + }, + { + "cell_type": "markdown", + "id": "580ee0f8-3b84-416b-ae84-ab87c1cd6bbe", + "metadata": {}, + "source": [ + "## Develop a Python function called rental_count_month " + ] + }, + { + "cell_type": "markdown", + "id": "25ddf5f8-bf6b-49d9-9f84-1a9d3ec58394", + "metadata": {}, + "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 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": 7, + "id": "9924e680-0d89-4f1b-b11c-600876e18a40", + "metadata": {}, + "outputs": [], + "source": [ + "def rental_count_month(df, month, year):\n", + "\n", + " column_name = f\"rentals_{month:02d}_{year}\" #02d means to have 2 digits\n", + "\n", + " result = df.groupby(\"customer_id\").size().reset_index(name = column_name)\n", + " \n", + " return result" + ] + }, + { + "cell_type": "markdown", + "id": "a7d8731d-b82a-47b3-9de6-2c4a9cc3f548", + "metadata": {}, + "source": [ + "### Test example with may 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "229cc51c-1c4d-488f-bd86-7e9f7cc89d28", + "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": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may_counts = rental_count_month(may, 5, 2005)\n", + "may_counts.head()" + ] + }, + { + "cell_type": "markdown", + "id": "dfd68030-5d0f-4783-a37d-3aeadc0ce247", + "metadata": {}, + "source": [ + "## Create a Python function called compare_rentals " + ] + }, + { + "cell_type": "markdown", + "id": "6082ca73-48f6-42af-a162-e3d535ee8f2c", + "metadata": {}, + "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 and years. \n", + "\n", + "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": 9, + "id": "4b68df15-dc33-45f7-87bb-080bd9254640", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals(df1, df2):\n", + " comparison = pd.merge(df1, df2, on=\"customer_id\",how=\"outer\").fillna(0)\n", + "\n", + " col1 = df1.columns[1]\n", + " col2 = df2.columns[1]\n", + "\n", + " comparison[\"difference\"]= comparison[col2] - comparison[col1]\n", + " \n", + "\n", + " return comparison" + ] + }, + { + "cell_type": "markdown", + "id": "a6b21889-5883-411c-9cb7-f4afa78c754f", + "metadata": {}, + "source": [ + "### Test example with may 2005 vs june 2005" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3406224e-166b-4885-bfe2-3066fd2a330f", + "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
012.07.05.0
121.01.00.0
232.04.02.0
340.06.06.0
453.05.02.0
\n", + "
" + ], + "text/plain": [ + " customer_id rentals_05_2005 rentals_06_2005 difference\n", + "0 1 2.0 7.0 5.0\n", + "1 2 1.0 1.0 0.0\n", + "2 3 2.0 4.0 2.0\n", + "3 4 0.0 6.0 6.0\n", + "4 5 3.0 5.0 2.0" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "may = rentals_month(engine, 5, 2005)\n", + "june = rentals_month(engine, 6, 2005)\n", + "\n", + "may_counts = rental_count_month(may, 5, 2005)\n", + "june_counts = rental_count_month(june, 6, 2005)\n", + "\n", + "comparison = compare_rentals(may_counts, june_counts)\n", + "\n", + "comparison.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3925c6f2-f1cf-42e7-8479-b56e46b66564", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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 +}