diff --git a/lab-sql-python-connection.ipynb b/lab-sql-python-connection.ipynb new file mode 100644 index 0000000..0c77f01 --- /dev/null +++ b/lab-sql-python-connection.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "id": "44a1386b-2c39-439e-b750-f465308f3592", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: sqlalchemy in c:\\users\\punk_\\anaconda3\\lib\\site-packages (2.0.43)\n", + "Requirement already satisfied: greenlet>=1 in c:\\users\\punk_\\anaconda3\\lib\\site-packages (from sqlalchemy) (3.2.4)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in c:\\users\\punk_\\anaconda3\\lib\\site-packages (from sqlalchemy) (4.15.0)\n" + ] + } + ], + "source": [ + "!pip install sqlalchemy" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "23d3cee8-248d-4fad-9463-35e55b08e138", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting pymysql\n", + " Downloading pymysql-1.1.2-py3-none-any.whl.metadata (4.3 kB)\n", + "Downloading pymysql-1.1.2-py3-none-any.whl (45 kB)\n", + "Installing collected packages: pymysql\n", + "Successfully installed pymysql-1.1.2\n" + ] + } + ], + "source": [ + "!pip install pymysql" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2409c6a1-5739-43c6-b4a6-863d9ccbcc88", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install pymysql sqlalchemy pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "70bdb1b5-b6bd-4489-9d9d-cc2ea215c8b8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + " ········\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "conection : sakila!\n" + ] + } + ], + "source": [ + "# 1. Establish a connection between Python and the Sakila database.\n", + "import pandas as pd\n", + "import numpy as np\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import getpass\n", + "\n", + "password = getpass.getpass() #ask for the password\n", + "bd = \"sakila\" #data base\n", + "\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/' + bd\n", + "engine = create_engine(connection_string)\n", + "\n", + "try:\n", + " with engine.connect() as connection:\n", + " print(f\"conection : {bd}!\")\n", + "except Exception as e:\n", + " print(f\"Error : {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "db4db652-2b60-4967-92de-7d9b00b0fcb9", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'df' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[21]\u001b[39m\u001b[32m, line 10\u001b[39m\n\u001b[32m 8\u001b[39m df = pd.read_sql(query, engine)\n\u001b[32m 9\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m df\n\u001b[32m---> \u001b[39m\u001b[32m10\u001b[39m \u001b[43mdf\u001b[49m\n", + "\u001b[31mNameError\u001b[39m: name 'df' is not defined" + ] + } + ], + "source": [ + "#2. Write a Python function called rentals_month \n", + "def rentals_month(engine, month, year):\n", + " query = f\"\"\"\n", + " SELECT * FROM rental \n", + " WHERE MONTH(rental_date) = {month} \n", + " AND YEAR(rental_date) = {year};\n", + " \"\"\"\n", + " df = pd.read_sql(query, engine)\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "056ef1a6-a6df-49ea-94f2-5a1041d6b519", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Develop a Python function called rental_count_month\n", + "def rental_count_month(df, month, year):\n", + " count_df = df.groupby('customer_id')[['rental_id']].count()\n", + " \n", + " column_name = f\"rentals_{month:02d}_{year}\"\n", + " count_df.columns = [column_name]\n", + " \n", + " return count_df.reset_index()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f469b4c-3318-4738-9e14-2f7575ebbad5", + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create a Python function called compare_rentals \n", + "def compare_rentals(df1, df2):\n", + " combined_df = pd.merge(df1, df2, on='customer_id', how='inner')\n", + " combined_df['difference'] = combined_df.iloc[:, 2] - combined_df.iloc[:, 1]\n", + " \n", + " return combined_df" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "28bdb8c4-e2d0-4a2c-a965-3c0fa66addc4", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'rental_count_month' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 3\u001b[39m df_june = rentals_month(engine, \u001b[32m6\u001b[39m, \u001b[32m2005\u001b[39m)\n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m# Transformar a conteos\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m may_counts = \u001b[43mrental_count_month\u001b[49m(df_may, \u001b[32m5\u001b[39m, \u001b[32m2005\u001b[39m)\n\u001b[32m 7\u001b[39m june_counts = rental_count_month(df_june, \u001b[32m6\u001b[39m, \u001b[32m2005\u001b[39m)\n\u001b[32m 9\u001b[39m \u001b[38;5;66;03m# Comparar y mostrar reporte\u001b[39;00m\n", + "\u001b[31mNameError\u001b[39m: name 'rental_count_month' is not defined" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74651bad-d6c3-41b0-99ff-9d485df60f54", + "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 +}