From 949f321790e1784b9e247318dc93b1f41d00b598 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Mon, 25 May 2026 15:04:04 +0200 Subject: [PATCH] solved --- sql-python-connection.ipynb | 718 ++++++++++++++++++++++++++++++++++++ 1 file changed, 718 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..8fcb9df --- /dev/null +++ b/sql-python-connection.ipynb @@ -0,0 +1,718 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 17, + "id": "55bb40a3-c8f4-4a0d-b9f5-ef81798d2306", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter password: ········\n" + ] + } + ], + "source": [ + "# 1. Establish a connection between Python and the Sakila database.\n", + "\n", + "\n", + "import pandas as pd\n", + "from sqlalchemy import create_engine\n", + "import getpass\n", + "from sqlalchemy import text\n", + "\n", + "password = getpass.getpass(\"Enter password: \")\n", + "\n", + "sk = \"sakila\"\n", + "\n", + "connection_string = f\"mysql+pymysql://root:{password}@localhost/{sk}\"\n", + "\n", + "engine = create_engine(connection_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "81367639-40f1-4da5-a0ee-846080a1d72c", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Write a Python function called rentals_month that retrieves rental data for a given month and year (passed as parameters) \n", + "# 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", + "# 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", + "\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", + " \n", + " df = pd.read_sql(query, engine)\n", + " \n", + " return df\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "0e092fde-23ec-4727-a1b2-9c2fbe32fef6", + "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": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1=rentals_month(engine, 5, 2005)\n", + "df1" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "1cfbbf95-438a-43db-92eb-5a7a386d0fa7", + "metadata": {}, + "outputs": [], + "source": [ + "# Develop a Python function called rental_count_month that takes the DataFrame provided by rentals_month as input \n", + "# along with the month and year and returns a new DataFrame containing the number of rentals made by each \n", + "# customer_id during the selected month and year.\n", + "\n", + "# The function should also include the month and year as parameters \n", + "# and use them to name the new column according to the month and year, \n", + "# for example, if the input month is 05 and the year is 2005, the column name should be \"rentals_05_2005\".\n", + "\n", + "def rental_count_month(df1, month, year):\n", + " column_name = f\"rentals_{month}_{year}\"\n", + "\n", + " result = (\n", + " df1.groupby(\"customer_id\")[\"rental_date\"].count().reset_index(name=column_name))\n", + " return result\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "cceb20a3-712b-4a3b-a960-eac50f4637aa", + "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_5_2005
012
121
232
353
463
.........
5155944
5165951
5175966
5185972
5195991
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_5_2005\n", + "0 1 2\n", + "1 2 1\n", + "2 3 2\n", + "3 5 3\n", + "4 6 3\n", + ".. ... ...\n", + "515 594 4\n", + "516 595 1\n", + "517 596 6\n", + "518 597 2\n", + "519 599 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2=rental_count_month(df1, 5, 2005)\n", + "df2\n" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "703b28db-5e93-4750-b983-73c3b6d2f44b", + "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_7_2005
012
121
232
353
463
.........
5155944
5165951
5175966
5185972
5195991
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_7_2005\n", + "0 1 2\n", + "1 2 1\n", + "2 3 2\n", + "3 5 3\n", + "4 6 3\n", + ".. ... ...\n", + "515 594 4\n", + "516 595 1\n", + "517 596 6\n", + "518 597 2\n", + "519 599 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3=rental_count_month(df1, 7, 2005)\n", + "df3\n" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "aa1c7288-9968-4b13-a49d-48609f9eef0b", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create a Python function called compare_rentals that takes two DataFrames as input \n", + "# containing the number of rentals made by each customer in different months and years. \n", + "# The function should return a combined DataFrame with a new 'difference' column, \n", + "# which is the difference between the number of rentals in the two months.\n", + "\n", + "\n", + "def compare_rentals(df2, df3):\n", + " \n", + " combined_df = pd.merge(df2, df3, on=\"customer_id\", how=\"outer\")\n", + " \n", + " col1 = combined_df.columns[1]\n", + " col2 = combined_df.columns[2]\n", + " \n", + " combined_df[\"difference\"] = combined_df[col2] - combined_df[col1]\n", + " \n", + " return combined_df\n" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "7bc9a3f1-d537-4d19-af24-3c3a08d72c98", + "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_5_2005rentals_7_2005difference
01220
12110
23220
35330
46330
...............
515594440
516595110
517596660
518597220
519599110
\n", + "

520 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_id rentals_5_2005 rentals_7_2005 difference\n", + "0 1 2 2 0\n", + "1 2 1 1 0\n", + "2 3 2 2 0\n", + "3 5 3 3 0\n", + "4 6 3 3 0\n", + ".. ... ... ... ...\n", + "515 594 4 4 0\n", + "516 595 1 1 0\n", + "517 596 6 6 0\n", + "518 597 2 2 0\n", + "519 599 1 1 0\n", + "\n", + "[520 rows x 4 columns]" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compare_rentals(df2, df3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7cd78329-b8a3-497d-8549-88ef1599202e", + "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.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}