{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "4d2f6331-3b21-45d8-a1d4-0f31471463ce", "metadata": {}, "outputs": [], "source": [ "from data201 import make_connection, dataframe_query" ] }, { "cell_type": "code", "execution_count": null, "id": "295c34b0-4708-4eca-9e24-b281eedfb98d", "metadata": {}, "outputs": [], "source": [ "conn = make_connection('titanic_2.ini')" ] }, { "cell_type": "markdown", "id": "829ae112-aacf-4ea2-8df4-bea2de1474df", "metadata": {}, "source": [ "# Create the young_men_survivors table." ] }, { "cell_type": "code", "execution_count": null, "id": "05d30dd3-5bfe-47ce-8086-92334d8fdf4e", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP TABLE IF EXISTS young_men_survivors')\n", "\n", "sql = \"\"\"\n", " CREATE TABLE young_men_survivors\n", " (\n", " name VARCHAR(32) NOT NULL UNIQUE,\n", " age DOUBLE NOT NULL,\n", " class INT NOT NULL,\n", " PRIMARY KEY(name)\n", " )\n", " \"\"\"\n", "\n", "cursor.execute(sql)" ] }, { "cell_type": "markdown", "id": "08eebdcd-1c6c-460e-a77d-243350c86fcf", "metadata": {}, "source": [ "# Load the young_men_survivors table." ] }, { "cell_type": "code", "execution_count": null, "id": "53e3671d-b155-485d-a90c-e977cd7e2be4", "metadata": {}, "outputs": [], "source": [ "sql = \"\"\"\n", " INSERT INTO young_men_survivors\n", " SELECT name, age,\n", " CASE \n", " WHEN passenger_class = '1st' THEN 1\n", " WHEN passenger_class = '2nd' THEN 2\n", " WHEN passenger_class = '3rd' THEN 3\n", " ELSE 0\n", " END AS class\n", " FROM passengers\n", " WHERE sex = 'male'\n", " AND survived = 'yes'\n", " AND age BETWEEN 21 AND 24\n", " \"\"\"\n", "\n", "cursor.execute(sql)\n", "conn.commit()" ] }, { "cell_type": "markdown", "id": "7a071f1a-99b5-43f8-b625-19aa95bb6fbe", "metadata": {}, "source": [ "# Display the table." ] }, { "cell_type": "code", "execution_count": null, "id": "77e3380d-2ee6-4769-a794-48a4a07bda31", "metadata": {}, "outputs": [], "source": [ "_, df = dataframe_query(conn, \n", " \"\"\"\n", " SELECT *\n", " FROM young_men_survivors\n", " ORDER BY class, age\n", " \"\"\"\n", " )\n", "display(df)" ] }, { "cell_type": "code", "execution_count": null, "id": "3d50dd07-7174-47a2-aba2-999ae303c8ef", "metadata": {}, "outputs": [], "source": [ "conn.close()" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }