Skip to content
Open
Show file tree
Hide file tree
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
388 changes: 388 additions & 0 deletions lab-sql-9.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,388 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "de221430",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\n!pip install mysql.connector\\n!pip install mysql.connector.python\\n!pip install sqlalchemy\\n!pip install pymysql\\n\\n'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"!pip install mysql.connector\n",
"!pip install mysql.connector.python\n",
"!pip install sqlalchemy\n",
"!pip install pymysql\n",
"\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "37d20b1b",
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"from sqlalchemy import create_engine\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b4268fa3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"········\n"
]
}
],
"source": [
"import getpass\n",
"password = getpass.getpass()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "1639585c",
"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_name</th>\n",
" <th>nr_rentals_may</th>\n",
" <th>nr_rentals_june</th>\n",
" </tr>\n",
" <tr>\n",
" <th>customer_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>197</th>\n",
" <td>SUE PETERS</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>176</th>\n",
" <td>JUNE CARROLL</td>\n",
" <td>5</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>371</th>\n",
" <td>BILLY POULIN</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>109</th>\n",
" <td>EDNA WEST</td>\n",
" <td>7</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>196</th>\n",
" <td>ALMA AUSTIN</td>\n",
" <td>4</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>440</th>\n",
" <td>BERNARD COLBY</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>487</th>\n",
" <td>HECTOR POINDEXTER</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>555</th>\n",
" <td>DWIGHT LOMBARDI</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>598</th>\n",
" <td>WADE DELVALLE</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>195</th>\n",
" <td>VANESSA SIMS</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>599 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" customer_name nr_rentals_may nr_rentals_june\n",
"customer_id \n",
"197 SUE PETERS 8 8\n",
"176 JUNE CARROLL 5 8\n",
"371 BILLY POULIN 6 7\n",
"109 EDNA WEST 7 5\n",
"196 ALMA AUSTIN 4 8\n",
"... ... ... ...\n",
"440 BERNARD COLBY 1 0\n",
"487 HECTOR POINDEXTER 0 1\n",
"555 DWIGHT LOMBARDI 0 1\n",
"598 WADE DELVALLE 0 1\n",
"195 VANESSA SIMS 0 0\n",
"\n",
"[599 rows x 3 columns]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"connection_string = 'mysql+pymysql://root:' + password + '@localhost/bank'\n",
"engine = create_engine(connection_string)\n",
"data = pd.read_sql_query('select * from sakila.rentals_may_june', engine)\n",
"data = data.set_index('customer_id')\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "6eae4c81",
"metadata": {},
"outputs": [],
"source": [
"def compare_rentals_may_june(df):\n",
" df['most_rentals'] = np.where(df['nr_rentals_may'] > df['nr_rentals_june'], 'may',\n",
" np.where(df['nr_rentals_may'] < df['nr_rentals_june'], 'june', 'equal'))\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "0e35377b",
"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_name</th>\n",
" <th>nr_rentals_may</th>\n",
" <th>nr_rentals_june</th>\n",
" <th>most_rentals</th>\n",
" </tr>\n",
" <tr>\n",
" <th>customer_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>197</th>\n",
" <td>SUE PETERS</td>\n",
" <td>8</td>\n",
" <td>8</td>\n",
" <td>equal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>176</th>\n",
" <td>JUNE CARROLL</td>\n",
" <td>5</td>\n",
" <td>8</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>371</th>\n",
" <td>BILLY POULIN</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>109</th>\n",
" <td>EDNA WEST</td>\n",
" <td>7</td>\n",
" <td>5</td>\n",
" <td>may</td>\n",
" </tr>\n",
" <tr>\n",
" <th>196</th>\n",
" <td>ALMA AUSTIN</td>\n",
" <td>4</td>\n",
" <td>8</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>440</th>\n",
" <td>BERNARD COLBY</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>may</td>\n",
" </tr>\n",
" <tr>\n",
" <th>487</th>\n",
" <td>HECTOR POINDEXTER</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>555</th>\n",
" <td>DWIGHT LOMBARDI</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>598</th>\n",
" <td>WADE DELVALLE</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>june</td>\n",
" </tr>\n",
" <tr>\n",
" <th>195</th>\n",
" <td>VANESSA SIMS</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>equal</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>599 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" customer_name nr_rentals_may nr_rentals_june most_rentals\n",
"customer_id \n",
"197 SUE PETERS 8 8 equal\n",
"176 JUNE CARROLL 5 8 june\n",
"371 BILLY POULIN 6 7 june\n",
"109 EDNA WEST 7 5 may\n",
"196 ALMA AUSTIN 4 8 june\n",
"... ... ... ... ...\n",
"440 BERNARD COLBY 1 0 may\n",
"487 HECTOR POINDEXTER 0 1 june\n",
"555 DWIGHT LOMBARDI 0 1 june\n",
"598 WADE DELVALLE 0 1 june\n",
"195 VANESSA SIMS 0 0 equal\n",
"\n",
"[599 rows x 4 columns]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compare_rentals_may_june(data)"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading