{ "cells": [ { "cell_type": "markdown", "id": "5967dd2e-c624-4367-9130-3035f1d029f2", "metadata": {}, "source": [ "# Query Results and Pandas Dataframes\n", "\n", "#### Pandas is a key Python library for data science. It is built upon NumPy, the library for doing numerical computations with matrices.\n", "\n", "#### The DataFrame is a principal data structure in the Pandas library:\n", "- It is a table with rows and columns.\n", "- It is an ideal structure to store query results.\n", "- It supports many data analytical functions" ] }, { "cell_type": "code", "execution_count": null, "id": "8c2769b7-ab08-4ad8-af36-6257935e2508", "metadata": {}, "outputs": [], "source": [ "from DATA225utils import make_connection\n", "from pandas import DataFrame" ] }, { "cell_type": "code", "execution_count": null, "id": "f0b2e68e-fa5c-4c0c-ab1f-6248d33897b0", "metadata": {}, "outputs": [], "source": [ "conn = make_connection('school.ini')\n", "cursor = conn.cursor()" ] }, { "cell_type": "markdown", "id": "6e46252d-f8aa-42e3-adb9-890d4a9d027e", "metadata": {}, "source": [ "### Who are the students of Mabel Flynn?" ] }, { "cell_type": "code", "execution_count": null, "id": "5642089a-dcc2-4c67-9eb1-3b218a002e88", "metadata": {}, "outputs": [], "source": [ "sql = ( \"\"\"\n", " SELECT student.first, student.last, subject \n", " FROM student, teacher, class, takes \n", " WHERE teacher.last = 'Flynn' \n", " AND teacher.first = 'Mabel' \n", " AND teacher_id = teacher.id \n", " AND code = class_code \n", " AND student_id = student.id \n", " ORDER BY subject, student.last\n", " \"\"\"\n", " )\n", "\n", "cursor.execute(sql)" ] }, { "cell_type": "code", "execution_count": null, "id": "2cc7ce8b-b61d-4fd8-b951-383a9c0eab52", "metadata": {}, "outputs": [], "source": [ "rows = cursor.fetchall()\n", "count = cursor.rowcount\n", "\n", "print(f'count = {count}')\n", "print()\n", "display(rows)" ] }, { "cell_type": "markdown", "id": "62f844d8-d8ee-498a-b62b-63a8e078ab6b", "metadata": {}, "source": [ "### Get the names of the columns using **list comprehension**." ] }, { "cell_type": "code", "execution_count": null, "id": "83d5aa74-023f-4bc6-a647-a7c2bc8f21a5", "metadata": {}, "outputs": [], "source": [ "columns = cursor.description\n", "column_names = [column_info[0] for column_info in columns]\n", "\n", "columns" ] }, { "cell_type": "markdown", "id": "12546759-9e01-475a-bd63-9e142baa08f9", "metadata": {}, "source": [ "### Get the names of the columns using a `for` loop." ] }, { "cell_type": "code", "execution_count": null, "id": "e4342520-53c0-404b-b7b6-22914c5968d8", "metadata": {}, "outputs": [], "source": [ "column_names = []\n", "for column_info in columns:\n", " column_names.append(column_info[0])\n", "\n", "column_names" ] }, { "cell_type": "markdown", "id": "a0cb8f58-a748-4953-934a-6da0d8530703", "metadata": {}, "source": [ "### Create and display the dataframe if there were rows." ] }, { "cell_type": "code", "execution_count": null, "id": "dd795411-b036-4e57-8c91-440e73949243", "metadata": {}, "outputs": [], "source": [ "if count > 0:\n", " df = DataFrame(rows)\n", " df.columns = column_names\n", " display(df)\n", " \n", "else:\n", " print('No students.')" ] }, { "cell_type": "code", "execution_count": null, "id": "ae113741-af4a-4336-93cb-1ad2709d15b6", "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "conn.close()" ] }, { "cell_type": "markdown", "id": "2d048de8-c8dd-46e7-9466-c6f0463bfee9", "metadata": {}, "source": [ "#### Fetching query results into a dataframe is a common operation we can add as another function in our Python database utilities file **`DATA225utils.py`**." ] }, { "cell_type": "code", "execution_count": null, "id": "266ccf4b-320f-4e8d-8b3c-9b51810eabc2", "metadata": {}, "outputs": [], "source": [ "# Copyright (c) 2023 by Ronald Mak" ] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }