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
190 changes: 190 additions & 0 deletions lab_sql_python_connection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "83bcd4b9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>customer_id</th>\n",
" <th>rentals_05_2005</th>\n",
" <th>rentals_06_2005</th>\n",
" <th>difference</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>7</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>2</td>\n",
" <td>4</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>5</td>\n",
" <td>3</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine\n",
"from urllib.parse import quote_plus\n",
"\n",
"password = quote_plus(\"Gazeb@85\")\n",
"\n",
"engine = create_engine(\n",
" f\"mysql+pymysql://root:{password}@localhost:3306/sakila\"\n",
")\n",
"\n",
"def rentals_month(engine, month, year):\n",
" query = \"\"\"\n",
" SELECT *\n",
" FROM rental\n",
" WHERE MONTH(rental_date) = %(month)s\n",
" AND YEAR(rental_date) = %(year)s;\n",
" \"\"\"\n",
" \n",
" return pd.read_sql(\n",
" query,\n",
" engine,\n",
" params={\"month\": month, \"year\": year}\n",
" )\n",
"\n",
"\n",
"def rental_count_month(rentals_df, month, year):\n",
" column_name = f\"rentals_{month:02d}_{year}\"\n",
" \n",
" rental_count = (\n",
" rentals_df\n",
" .groupby(\"customer_id\")\n",
" .size()\n",
" .reset_index(name=column_name)\n",
" )\n",
" \n",
" return rental_count\n",
"\n",
"\n",
"def compare_rentals(df_month_1, df_month_2):\n",
" comparison = pd.merge(\n",
" df_month_1,\n",
" df_month_2,\n",
" on=\"customer_id\",\n",
" how=\"inner\"\n",
" )\n",
" \n",
" rental_columns = [\n",
" col for col in comparison.columns \n",
" if col.startswith(\"rentals_\")\n",
" ]\n",
" \n",
" comparison[\"difference\"] = (\n",
" comparison[rental_columns[1]] - comparison[rental_columns[0]]\n",
" )\n",
" \n",
" return comparison\n",
"\n",
"\n",
"rentals_may = rentals_month(engine, 5, 2005)\n",
"rentals_june = rentals_month(engine, 6, 2005)\n",
"\n",
"rentals_may_count = rental_count_month(rentals_may, 5, 2005)\n",
"rentals_june_count = rental_count_month(rentals_june, 6, 2005)\n",
"\n",
"comparison_df = compare_rentals(rentals_may_count, rentals_june_count)\n",
"\n",
"comparison_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1929046c",
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}