Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 293 additions & 0 deletions lab_sql_python_connection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b559aa40",
"metadata": {},
"source": [
"![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)\n",
"\n",
"# LAB | Connecting Python to SQL\n",
"\n",
"<details>\n",
" <summary>\n",
" <h2>Learning Goals</h2>\n",
" </summary>\n",
"\n",
" This lab allows you to practice and apply the concepts and techniques taught in class. \n",
"\n",
" Upon completion of this lab, you will be able to:\n",
" \n",
"- Write a Python script to connect to a relational database using the appropriate Python library and query it using SQL commands.\n",
"\n",
" <br>\n",
" <hr> \n",
"\n",
"</details>\n",
"\n",
"<details>\n",
" <summary>\n",
" <h2>Prerequisites</h2>\n",
" </summary>\n",
"\n",
"Before this starting this lab, you should have learnt about:\n",
"\n",
"- Basic SQL queries\n",
"- Python and Pandas \n",
"- Data Wrangling, which involves tasks such as grouping, aggregating, dealing with indexes, renaming columns, merging data and performing calculations.\n",
" \n",
" <br>\n",
" <hr> \n",
"\n",
"</details>\n",
"\n",
"\n",
"## Introduction\n",
"\n",
"Welcome to the Connecting Python to SQL lab!\n",
"\n",
"In this lab, you will be working with the [Sakila](https://dev.mysql.com/doc/sakila/en/) database on movie rentals. Specifically, you will be practicing how to do basic SQL queries using Python. By connecting Python to SQL, you can leverage the power of both languages to efficiently manipulate and analyze large datasets. Throughout this lab, you will practice how to use Python to retrieve and manipulate data stored in the Sakila database using SQL queries. Let's get started!\n",
"\n",
"## Challenge\n",
"\n",
"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. To achieve this, follow these steps:\n",
"\n",
"1. Establish a connection between Python and the Sakila database.\n",
"\n",
"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. The function should take in three parameters:\n",
"\n",
"\t- `engine`: an object representing the database connection engine to be used to establish a connection to the Sakila database.\n",
"\t- `month`: an integer representing the month for which rental data is to be retrieved.\n",
"\t- `year`: an integer representing the year for which rental data is to be retrieved.\n",
"\n",
"\tThe 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.\n",
"\n",
"3. 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",
"\tThe 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",
"\n",
"\t*Hint: Consider making use of pandas [groupby()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)*\n",
"\n",
"4. 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",
"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.\n",
"\n",
"## Requirements\n",
"\n",
"- Fork this repo\n",
"- Clone it to your machine\n",
"\n",
"\n",
"\n",
"## Getting Started\n",
"\n",
"Complete the challenges. Follow the instructions and add your code and explanations as necessary.\n",
"\n",
"## Submission\n",
"\n",
"- Upon completion, run the following commands:\n",
"\n",
"```bash\n",
"git add .\n",
"git commit -m \"Solved lab\"\n",
"git push origin master\n",
"```\n",
"\n",
"- Paste the link of your lab in Student Portal.\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78026090",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import pymysql\n",
"from sqlalchemy import create_engine,text\n",
"import getpass # To get the password without showing the input"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbc2a59c",
"metadata": {},
"outputs": [],
"source": [
"# Credenciales de acceso\n",
"db_user = \"root\"\n",
"db_password = getpass.getpass(\"Por favor, introduce la contraseña de tu base de datos MySQL: \")\n",
"db_host = \"localhost\"\n",
"db_port = \"3306\"\n",
"db_name = \"sakila\"\n",
"\n",
"# Construcción de la URL de conexión para SQLAlchemy\n",
"database_url = f\"mysql+pymysql://{db_user}:{db_password}@{db_host}:{db_port}/\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfd72af4",
"metadata": {},
"outputs": [],
"source": [
"'''\n",
"Estableces conexión entre python y el sistema de manejo de bases de datos relacionales (MySQL)\n",
"y ejecutas consultas que almacenas en una variable que puedes tratar con python.\n",
"\n",
"El engine (motor) de SQLAlchemy es el objeto que sabe cómo conectarse a la base de datos (conoce la URL, el usuario, la contraseña y el puerto). \n",
"Sin embargo, el engine por sí solo no abre la conexión inmediatamente. Es simplemente el aparato telefónico en tu mesa.\n",
"\n",
"engine.connect(): En el momento en que se ejecuta esta línea, Python \"descuelga el teléfono\" y establece una conexión real con MySQL. A esa llamada abierta la llamamos connection.\n",
"'''\n",
"engine = create_engine(database_url) # Compras el teléfono y anotas el número de la oficina.\n",
"try:\n",
" with engine.connect() as connection:\n",
" # Con text traduces tu mensaje al idioma de SLQ \n",
" # Con connection.execute() dices el mensaje. El método .execute() es el que envía el comando SQL directamente al servidor de MySQL.\n",
" # Con result escuchas y anotas la respuesta. \n",
" result = connection.execute(text('SELECT 1')) \n",
" '''\n",
" La respuesta es un objeto especial de SQLAlchemy que contiene filas. \n",
" Puedes recorrerlo con un bucle for, o usar .scalar() (si solo devuelve un número) para ver lo que la base de datos te ha respondido.\n",
" '''\n",
" print(f'Conexión exitosa! El servidor respondió: {result.scalar()}')\n",
"except Exception as e:\n",
" print(\"❌ Error al conectar a la base de datos: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81c8dbac",
"metadata": {},
"outputs": [],
"source": [
"#construimos la url que apunta a una db concreta en nuestro SQL\n",
"db_engine = create_engine(f'{database_url}{db_name}') \n",
"try:\n",
" with db_engine.connect() as connection:\n",
" result = connection.execute(text(\"SHOW TABLES;\"))\n",
" print(f\"¡Conexión exitosa! Estas son tus tablas en {db_name}:\")\n",
" for row in result:\n",
" print(f\"- {row[0]}\")\n",
"except Exception as e:\n",
" print(\"❌ No se pudo conectar al servidor de MySQL:\")\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"id": "27f6da3f",
"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. The function should take in three parameters:\n",
"\n",
"\t- `engine`: an object representing the database connection engine to be used to establish a connection to the Sakila database.\n",
"\t- `month`: an integer representing the month for which rental data is to be retrieved.\n",
"\t- `year`: an integer representing the year for which rental data is to be retrieved."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e1438e6",
"metadata": {},
"outputs": [],
"source": [
"def rentals_month(engine,month,year):\n",
" with engine.connect() as connection:\n",
" # Getting how many loans were granted every year, and the month of each duration.\n",
" txt = f'select * from rental where rental_date like \"%{year}-{month}%\";'\n",
" query = text(txt)\n",
" result = connection.execute(query).mappings().all()\n",
" return pd.DataFrame(result)"
]
},
{
"cell_type": "markdown",
"id": "d40f52f3",
"metadata": {},
"source": [
"3. 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",
"\tThe 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",
"\n",
"\t*Hint: Consider making use of pandas [groupby()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)*"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27c1a28b",
"metadata": {},
"outputs": [],
"source": [
"def rental_count_month(rentals_month,month,year):\n",
" df = rentals_month(db_engine,month,year)\n",
" df_rental = df.groupby('customer_id')['rental_id'].count().reset_index()\n",
" df_rental = df_rental.rename(columns={'rental_id': f'rentals-{month}-{year}'})\n",
" return df_rental"
]
},
{
"cell_type": "markdown",
"id": "213f9610",
"metadata": {},
"source": [
"4. 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",
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a074d204",
"metadata": {},
"outputs": [],
"source": [
"def compare_rentals(df_1, df_2):\n",
" difference_df = pd.merge(df_1,df_2, on='customer_id')\n",
" difference_df['difference'] = difference_df.iloc[:,1]-difference_df.iloc[:,2]\n",
" return difference_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d39d190",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}