{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9688e2e9-87d6-420c-b76f-ff61fbd19694",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Multiple Conditions\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the `kind` is `'sporting'` and the `weight` is greater than 20.\n",
    "Answer:\n",
    "dogs[(dogs['kind'] == 'sporting') & (dogs['weight'] > 20)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eeb8b201",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# You'll start seeing this cell in most lectures.\n",
    "# It exists to hide all of the import statements and other setup\n",
    "# code we need in lecture notebooks.\n",
    "from dsc80_utils import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3143de48",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Lecture 2 – DataFrame Fundamentals\n",
    "\n",
    "## DSC 80, Winter 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a0d867d",
   "metadata": {
    "editable": true,
    "jp-MarkdownHeadingCollapsed": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Announcements 📣\n",
    "\n",
    "- Lab 1 is released, and is due **Wednesday, January 15 at 11:59pm**.\n",
    "    - See the [Tech Support](https://dsc80.com/tech_support/) page for instructions and watch [this video 🎥](https://www.loom.com/share/0ea254b85b2745e59322b5e5a8692e91?sid=d64a6a06-b2ca-4290-babc-be6666425d09) for tips on how to set up your environment and work on assignments.\n",
    "    - Please try to set up your computer ASAP so that you have enough time to debug your environment.\n",
    "- Project 1 will be released by Wednesday.\n",
    "- Please fill out the [Welcome Survey](https://forms.gle/uYGK8tFj8EygXmERA) **ASAP**.\n",
    "- Lecture recordings are available [here](https://podcast.ucsd.edu), and are linked on the course website."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a4afdd4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Agenda\n",
    "\n",
    "- `numpy` arrays.\n",
    "- From `babypandas` to `pandas`.\n",
    "    - Deep dive into DataFrames.\n",
    "- Accessing subsets of rows and columns in DataFrames.\n",
    "    - `.loc` and `.iloc`.\n",
    "    - Querying (i.e. filtering).\n",
    "- Adding and modifying columns.\n",
    "- `pandas` and `numpy`.\n",
    "    \n",
    "We can't cover every single detail! The [`pandas` documentation](https://pandas.pydata.org/docs/user_guide/index.html#user-guide) will be your friend."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50e5db5f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "    <h3>Question 🤔 (Answer using `lec02-dog`)</h3>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c7f4b593",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           breed      kind  lifetime_cost  longevity    size  weight  height\n",
       "0       Brittany  sporting        22589.0      12.92  medium    35.0    19.0\n",
       "1  Cairn Terrier   terrier        21992.0      13.84   small    14.0    10.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs = pd.read_csv('data/dogs43.csv')\n",
    "dogs.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22f305a2-dbae-4fbc-8324-004eee850a16",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### 7. Visualization\n",
    "\n",
    "This chapter explores graphical methods of data analysis using the Internet Movie Database. It covers scatter plots, line graphs, and categorical distributions for visualization and interpreting data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "520ceee8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "**What does this code do?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "11978ecf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "size\n",
       "large     12.42\n",
       "medium    12.58\n",
       "small     16.50\n",
       "Name: longevity, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "whoa = np.random.choice([True, False], size=len(dogs))\n",
    "(dogs[whoa]\n",
    " .groupby('size')\n",
    " .max()\n",
    " .get('longevity')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaecf86a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## `numpy` arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4dc9df4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### `numpy` overview\n",
    "\n",
    "- `numpy` stands for \"numerical Python\". It is a commonly-used Python module that enables **fast** computation involving arrays and matrices.\n",
    "- `numpy`'s main object is the **array**. In `numpy`, arrays are:\n",
    "    - Homogenous – all values are of the same type.\n",
    "    - (Potentially) multi-dimensional.\n",
    "- Computation in `numpy` is fast because:\n",
    "    - Much of it is implemented in C.\n",
    "    - `numpy` arrays are stored more efficiently in memory than, say, Python lists. \n",
    "- [This site](https://cloudxlab.com/blog/numpy-pandas-introduction/) provides a good overview of `numpy` arrays.\n",
    "\n",
    "We used `numpy` in DSC 10 to work with sequences of data:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12c9bda7-8611-44fa-905b-6ea9affc070f",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Understanding DataFrames\n",
    "\n",
    "Question:\n",
    "Explain the fundamental structure of a pandas DataFrame, including its key components (rows, columns, index) and how these components relate to each other.  What are the advantages of using DataFrames for data analysis compared to other data structures like lists of lists?\n",
    "Answer:\n",
    "A pandas DataFrame is a two-dimensional, size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).  Its key components are:\n",
    "\n",
    "* **Rows:** Represent individual observations or records.  Each row typically corresponds to a single unit of data.\n",
    "* **Columns:** Represent variables or attributes of the data.  Each column contains data of a similar type (though it's possible to have mixed types, albeit inefficiently).\n",
    "* **Index:** A sequence of labels that uniquely identify each row.  This enables efficient access to rows by their label instead of their numerical position.\n",
    "\n",
    "Advantages of DataFrames over other data structures:\n",
    "\n",
    "* **Structure:** DataFrames provide a clear and organized structure, making it easier to manage and analyze large datasets.  This structure contrasts with the unstructured nature of nested lists.\n",
    "* **Labeling:** The labeled axes (rows and columns) in DataFrames improve readability and facilitate data manipulation using names instead of integer indices. Lists of lists lack this labeling.\n",
    "* **Efficiency:** Pandas utilizes NumPy's underlying implementation for numerical operations, resulting in efficient computation, particularly with vectorized operations.  NumPy arrays are also more memory-efficient than lists.\n",
    "* **Functionality:** Pandas provides a wealth of built-in functions for data cleaning, transformation, analysis, and visualization, simplifying many common data manipulation tasks. Lists of lists lack these built-in functionalities.\n",
    "* **Heterogeneity (to an extent):** While DataFrames ideally have columns of homogeneous type, pandas allows for more flexibility in data types compared to NumPy arrays which are entirely homogeneous. This flexibility, while sometimes inefficient, can be more accommodating for real-world data. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91a2cf01-b1d8-4dfe-bf50-142e2c9ff808",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### NumPy Introduction\n",
    "\n",
    "NumPy is a Python library for working with arrays, offering functions for linear algebra, Fourier transforms, and matrices.  Created in 2005, it's open-source and significantly faster than Python lists due to efficient memory storage and optimization for modern CPUs.  It's written in Python, C, and C++, and its codebase is on GitHub."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47352801-9046-4aee-87d5-d390873efb4b",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Understanding set_index() with Duplicates\n",
    "\n",
    "Question:\n",
    "What happens if you have duplicate values in a column and use `set_index()` on it?\n",
    "Answer:\n",
    "If you use `set_index()` on a column with duplicate values, pandas will raise an error because the index must contain unique values.  Each index value must uniquely identify a row.  Attempting to create an index with duplicates violates this constraint."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "80a82e41",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ed29adc4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The shape (10,) means that the array only has a single dimension,\n",
    "# of size 10.\n",
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "de9f257f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   4,   8,  16,  32,  64, 128, 256, 512])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84a43343",
   "metadata": {},
   "source": [
    "Arrays come equipped with several handy methods; some examples are below, but you can read about them all [here](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "df2ec76f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(1023)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 ** arr).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "77dfc0a2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(102.3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 ** arr).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eabeecaf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(512)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 ** arr).max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ca4bab47",
   "metadata": {
    "editable": true,
    "scrolled": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(9)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(2 ** arr).argmax()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb19cca8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### ⚠️ The dangers of `for`-loops\n",
    "\n",
    "- `for`-loops are slow when processing large datasets. **You will rarely write `for`-loops in DSC 80 (except for Lab 1 and Project 1), and may be penalized on assignments for using them when unnecessary!**\n",
    "- One of the biggest benefits of `numpy` is that it supports **vectorized** operations. \n",
    "    - If `a` and `b` are two arrays of the same length, then `a + b` is a new array of the same length containing the element-wise sum of `a` and `b`.\n",
    "- To illustrate how much faster `numpy` arithmetic is than using a `for`-loop, let's compute the squares of the numbers between 0 and 1,000,000:\n",
    "    - Using a `for`-loop.\n",
    "    - Using vectorized arithmetic, through `numpy`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2f0c5235",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29.3 ms ± 274 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "squares = []\n",
    "for i in range(1_000_000):\n",
    "    squares.append(i * i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1c026a9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "In vanilla Python, this takes about 0.04 seconds per loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1dae0772",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.26 ms ± 95.7 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "squares = np.arange(1_000_000) ** 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07b6d506",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "In `numpy`, this only takes about 0.001 seconds per loop, more than 40x faster! Note that under the hood, `numpy` is also using a `for`-loop, but it's a `for`-loop implemented in C, which is much faster than Python."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b602e6e6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### Multi-dimensional arrays\n",
    "\n",
    "While we didn't see these very often in DSC 10, multi-dimensional lists/arrays may have since come up in DSC 20, 30, or 40A (especially in the context of linear algebra).\n",
    "\n",
    "We'll spend a bit of time talking about 2D (and 3D) arrays here, since in some ways, they behave similarly to DataFrames. \n",
    "\n",
    "Below, we create a 2D array from scratch."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "96fdb9b9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 9, 7],\n",
       "       [9, 8, 2, 3],\n",
       "       [2, 5, 0, 4]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = np.array([\n",
    "    [5, 1, 9, 7],\n",
    "    [9, 8, 2, 3],\n",
    "    [2, 5, 0, 4]\n",
    "])\n",
    "\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6fa8e7e4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nums has 3 rows and 4 columns.\n",
    "nums.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78f49585",
   "metadata": {},
   "source": [
    "We can also create 2D arrays by _reshaping_ other arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b5574e1d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Here, we're asking to reshape np.arange(1, 7)\n",
    "# so that it has 2 rows and 3 columns.\n",
    "a = np.arange(1, 7).reshape((2, 3))\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18337230",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### Operations along axes\n",
    "\n",
    "In 2D arrays (and DataFrames), axis 0 refers to the rows (up and down) and axis 1 refers to the columns (left and right).\n",
    "\n",
    "<center><img src='imgs/axis-sum.png' width=600></center>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "926e7e95",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c93a42",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "If we specify `axis=0`, `a.sum` will \"compress\" along axis 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "94995957",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d58fb1b3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "If we specify `axis=1`, `a.sum` will \"compress\" along axis 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8a251862",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "718f6fbc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### Selecting rows and columns from 2D arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46b5b4b2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "You can use `[`square brackets`]` to **slice** rows and columns out of an array, using the same slicing conventions you saw in DSC 20."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6934d0fa",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "eb2e554b",
   "metadata": {
    "editable": true,
    "scrolled": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Accesses row 0 and all columns.\n",
    "a[0, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "65e4fdcf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Same as the above.\n",
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "195f2e35",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Accesses all rows and column 1.\n",
    "a[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7a049722",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Accesses row 0 and columns 1 and onwards.\n",
    "a[0, 1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "919f52a6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "    <h3>Question 🤔 (Answer at `lec02-grid`)</h3>\n",
    "        \n",
    "Try and predict the value of <code>grid[-1, 1:].sum()</code> without running the code below.\n",
    "\n",
    "<b>See if <a href=\"https://chat.openai.com/\">ChatGPT</a> can do it.</b>\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0a385d42",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "s = (5, 3)\n",
    "grid = np.ones(s) * 2 * np.arange(1, 16).reshape(s)\n",
    "# grid[-1, 1:].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b4b36a2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Example: Image processing\n",
    "\n",
    "`numpy` arrays are homogenous and potentially multi-dimensional.\n",
    "\n",
    "It turns out that **images** can be represented as 3D `numpy` arrays. The color of each pixel can be described with three numbers under the RGB model – a red value, green value, and blue value. Each of these can vary from 0 to 1.\n",
    "\n",
    "<center><img src='imgs/three_d_array.png' width=250><small>(<a href=\"https://e2eml.school/convert_rgb_to_grayscale\">image source</a>)</small></center>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4ef1666f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "img_path = Path('imgs') / 'bentley.jpg'\n",
    "img = np.asarray(Image.open(img_path)) / 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c3c05170",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.4 , 0.33, 0.24],\n",
       "        [0.42, 0.35, 0.25],\n",
       "        [0.43, 0.36, 0.26],\n",
       "        ...,\n",
       "        [0.5 , 0.44, 0.36],\n",
       "        [0.51, 0.44, 0.36],\n",
       "        [0.51, 0.44, 0.36]],\n",
       "\n",
       "       [[0.39, 0.33, 0.23],\n",
       "        [0.42, 0.36, 0.26],\n",
       "        [0.44, 0.37, 0.27],\n",
       "        ...,\n",
       "        [0.51, 0.44, 0.36],\n",
       "        [0.52, 0.45, 0.37],\n",
       "        [0.52, 0.45, 0.38]],\n",
       "\n",
       "       [[0.38, 0.31, 0.21],\n",
       "        [0.41, 0.35, 0.24],\n",
       "        [0.44, 0.37, 0.27],\n",
       "        ...,\n",
       "        [0.52, 0.45, 0.38],\n",
       "        [0.53, 0.46, 0.39],\n",
       "        [0.53, 0.47, 0.4 ]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[0.71, 0.64, 0.55],\n",
       "        [0.71, 0.65, 0.55],\n",
       "        [0.68, 0.62, 0.52],\n",
       "        ...,\n",
       "        [0.58, 0.49, 0.41],\n",
       "        [0.56, 0.47, 0.39],\n",
       "        [0.56, 0.47, 0.39]],\n",
       "\n",
       "       [[0.5 , 0.44, 0.34],\n",
       "        [0.42, 0.37, 0.26],\n",
       "        [0.44, 0.38, 0.28],\n",
       "        ...,\n",
       "        [0.4 , 0.33, 0.25],\n",
       "        [0.55, 0.48, 0.4 ],\n",
       "        [0.58, 0.5 , 0.42]],\n",
       "\n",
       "       [[0.38, 0.33, 0.22],\n",
       "        [0.49, 0.44, 0.33],\n",
       "        [0.56, 0.51, 0.4 ],\n",
       "        ...,\n",
       "        [0.15, 0.08, 0.  ],\n",
       "        [0.28, 0.21, 0.13],\n",
       "        [0.42, 0.35, 0.27]]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "405c5f82",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200, 263, 3)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "80128938",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"378.918pt\" height=\"291.6pt\" viewBox=\"0 0 378.918 291.6\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2025-01-08T20:35:07.441226</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 291.6 \n",
       "L 378.918 291.6 \n",
       "L 378.918 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g clip-path=\"url(#p56568b8383)\">\n",
       "    <image xlink:href=\"data:image/png;base64,\n",
       "[encoded data, 398104 characters]\" id=\"imagec86205f8b3\" transform=\"scale(1 -1) translate(0 -277.92)\" x=\"7.2\" y=\"-6.48\" width=\"365.04\" height=\"277.92\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p56568b8383\">\n",
       "   <rect x=\"7.2\" y=\"7.2\" width=\"364.518\" height=\"277.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(img)\n",
    "plt.axis('off');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c6b0383",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Applying a greyscale filter\n",
    "\n",
    "One way to convert an image to greyscale is to average its red, green, and blue values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "59d5a3a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.32, 0.34, 0.35, ..., 0.43, 0.44, 0.44],\n",
       "       [0.31, 0.35, 0.36, ..., 0.44, 0.45, 0.45],\n",
       "       [0.3 , 0.33, 0.36, ..., 0.45, 0.46, 0.47],\n",
       "       ...,\n",
       "       [0.64, 0.64, 0.6 , ..., 0.49, 0.47, 0.47],\n",
       "       [0.43, 0.35, 0.37, ..., 0.32, 0.48, 0.5 ],\n",
       "       [0.31, 0.42, 0.49, ..., 0.07, 0.21, 0.34]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_2d = img.mean(axis=2)\n",
    "mean_2d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f3f8ba",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "This is just a single red channel!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "773e9aa3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"378.918pt\" height=\"291.6pt\" viewBox=\"0 0 378.918 291.6\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2025-01-08T20:35:07.551041</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 291.6 \n",
       "L 378.918 291.6 \n",
       "L 378.918 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g clip-path=\"url(#p21052a8578)\">\n",
       "    <image xlink:href=\"data:image/png;base64,\n",
       "[encoded data, 384820 characters]\" id=\"image5d545a224b\" transform=\"scale(1 -1) translate(0 -277.92)\" x=\"7.2\" y=\"-6.48\" width=\"365.04\" height=\"277.92\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p21052a8578\">\n",
       "   <rect x=\"7.2\" y=\"7.2\" width=\"364.518\" height=\"277.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(mean_2d)\n",
    "plt.axis('off');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad0bed73",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "We need to _repeat_ `mean_2d` three times along axis 2, to use the same values for the red, green, and blue channels. `np.repeat` will help us here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a6b025bf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.newaxis is an alias for None.\n",
    "# It helps us introduce an additional axis.\n",
    "np.arange(5)[:, np.newaxis]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7e0a6db8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3],\n",
       "       [4, 4, 4]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(np.arange(5)[:, np.newaxis], 3, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "841a0699",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "mean_3d = np.repeat(mean_2d[:, :, np.newaxis], 3, axis=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "eee2cf26",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"378.918pt\" height=\"291.6pt\" viewBox=\"0 0 378.918 291.6\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2025-01-08T20:35:07.653522</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 291.6 \n",
       "L 378.918 291.6 \n",
       "L 378.918 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g clip-path=\"url(#pf33d966418)\">\n",
       "    <image xlink:href=\"data:image/png;base64,\n",
       "[encoded data, 214640 characters]\" id=\"imageb5259fa139\" transform=\"scale(1 -1) translate(0 -277.92)\" x=\"7.2\" y=\"-6.48\" width=\"365.04\" height=\"277.92\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"pf33d966418\">\n",
       "   <rect x=\"7.2\" y=\"7.2\" width=\"364.518\" height=\"277.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(mean_3d)\n",
    "plt.axis('off');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2633b72",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Applying a sepia filter\n",
    "\n",
    "Let's sepia-fy Junior!\n",
    "\n",
    "<center>\n",
    "<img src=\"imgs/apple-sepia.png\" width=50%>\n",
    "    <small>\n",
    "(<a href=\"https://support.apple.com/guide/motion/sepia-filter-motn169f8c87/mac\">Image credits</a>)</small>\n",
    "</center>\n",
    "\n",
    "From [here](https://stackoverflow.com/questions/1061093/how-is-a-sepia-tone-created), we can apply this conversion to each pixel.\n",
    "\n",
    "$$\\begin{align*}\n",
    "R_{\\text{sepia}} &= 0.393R + 0.769G + 0.189B \\\\ G_{\\text{sepia}} &= 0.349R + 0.686G + 0.168B \\\\\n",
    "B_{\\text{sepia}} &= 0.272R + 0.534G + 0.131B\\end{align*}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d0be87f2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sepia_filter = np.array([\n",
    "    [0.393, 0.769, 0.189],\n",
    "    [0.349, 0.686, 0.168],\n",
    "    [0.272, 0.534, 0.131]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9ed3e3f5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.46, 0.41, 0.32],\n",
       "        [0.48, 0.43, 0.33],\n",
       "        [0.5 , 0.44, 0.35],\n",
       "        ...,\n",
       "        [0.6 , 0.53, 0.42],\n",
       "        [0.6 , 0.54, 0.42],\n",
       "        [0.61, 0.54, 0.42]],\n",
       "\n",
       "       [[0.45, 0.4 , 0.31],\n",
       "        [0.49, 0.43, 0.34],\n",
       "        [0.5 , 0.45, 0.35],\n",
       "        ...,\n",
       "        [0.61, 0.54, 0.42],\n",
       "        [0.62, 0.55, 0.43],\n",
       "        [0.63, 0.56, 0.43]],\n",
       "\n",
       "       [[0.43, 0.38, 0.3 ],\n",
       "        [0.47, 0.42, 0.33],\n",
       "        [0.51, 0.45, 0.35],\n",
       "        ...,\n",
       "        [0.63, 0.56, 0.44],\n",
       "        [0.64, 0.57, 0.44],\n",
       "        [0.64, 0.57, 0.45]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[0.88, 0.78, 0.61],\n",
       "        [0.89, 0.79, 0.61],\n",
       "        [0.84, 0.75, 0.58],\n",
       "        ...,\n",
       "        [0.68, 0.61, 0.47],\n",
       "        [0.65, 0.58, 0.45],\n",
       "        [0.65, 0.58, 0.45]],\n",
       "\n",
       "       [[0.6 , 0.53, 0.42],\n",
       "        [0.5 , 0.44, 0.35],\n",
       "        [0.52, 0.46, 0.36],\n",
       "        ...,\n",
       "        [0.45, 0.4 , 0.31],\n",
       "        [0.66, 0.59, 0.46],\n",
       "        [0.69, 0.62, 0.48]],\n",
       "\n",
       "       [[0.45, 0.4 , 0.31],\n",
       "        [0.59, 0.53, 0.41],\n",
       "        [0.69, 0.61, 0.48],\n",
       "        ...,\n",
       "        [0.12, 0.1 , 0.08],\n",
       "        [0.3 , 0.26, 0.21],\n",
       "        [0.48, 0.43, 0.33]]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Multiplies each pixel by the sepia_filter matrix.\n",
    "# Then, clips each RGB value to be between 0 and 1.\n",
    "filtered = (img @ sepia_filter.T).clip(0, 1)\n",
    "filtered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2d58c595",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"378.918pt\" height=\"291.6pt\" viewBox=\"0 0 378.918 291.6\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2025-01-08T20:35:07.725678</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 291.6 \n",
       "L 378.918 291.6 \n",
       "L 378.918 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g clip-path=\"url(#p07b8a51fe9)\">\n",
       "    <image xlink:href=\"data:image/png;base64,\n",
       "[encoded data, 365216 characters]\" id=\"image9b90d1191d\" transform=\"scale(1 -1) translate(0 -277.92)\" x=\"7.2\" y=\"-6.48\" width=\"365.04\" height=\"277.92\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p07b8a51fe9\">\n",
       "   <rect x=\"7.2\" y=\"7.2\" width=\"364.518\" height=\"277.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(filtered)\n",
    "plt.axis('off');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e091cbf4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Key takeaway: avoid `for`-loops whenever possible!\n",
    "\n",
    "You can do a lot without `for`-loops, both in `numpy` and in `pandas`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "018d58fd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## From `babypandas` to `pandas` 🐼"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "420e9507",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `babypandas`\n",
    "\n",
    "In DSC 10, you used `babypandas`, which was a subset of `pandas` designed to be friendly for beginners.\n",
    "<center><img src='imgs/babypanda.jpg' width=45%></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8412e023",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### pandas\n",
    "\n",
    "You're not a beginner anymore – you've taken DSC 20, 30, and 40A. You're ready for the real deal.\n",
    "\n",
    "<center><img src='imgs/angrypanda.jpg' width=60%></center>\n",
    "\n",
    "Fortunately, **everything you learned in `babypandas` will carry over!**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7137020",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `pandas`\n",
    "\n",
    "<center><img src='imgs/pandas.png' width=200></center>\n",
    "\n",
    "- `pandas` is **the** Python library for tabular data manipulation.\n",
    "- Before `pandas` was developed, the standard data science workflow involved using multiple languages (Python, R, Java) in a single project.\n",
    "- Wes McKinney, the original developer of `pandas`, wanted a library which would allow everything to be done in Python.\n",
    "    - Python is faster to develop in than Java, and is more general-purpose than R."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a959a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `pandas` data structures\n",
    "\n",
    "There are three key data structures at the core of `pandas`:\n",
    "- DataFrame: 2 dimensional tables.\n",
    "- Series: 1 dimensional array-like object, typically representing a column or row.\n",
    "- Index: sequence of column or row labels.\n",
    "\n",
    "<center>\n",
    "    <img src='imgs/example-df.png' width=400>\n",
    "    <i>A DataFrame you'll see in Lab 1.</i>\n",
    "</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1129f156",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Importing `pandas` and related libraries\n",
    "\n",
    "`pandas` is almost always imported in conjunction with `numpy`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f14fcafa-a170-4c65-8921-9ccb867684a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09f320d6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Example: Dog Breeds (woof!) 🐶\n",
    "\n",
    "The dataset we'll work comes from the American Kennel Club. Here's a [cool plot](https://informationisbeautiful.net/visualizations/best-in-show-whats-the-top-data-dog/) made using our dataset.\n",
    "\n",
    "\n",
    "<center>\n",
    "    <img src=\"https://infobeautiful4.s3.amazonaws.com/2014/11/IIB_Best-In-Show_1276x2.png\" width=800>\n",
    "</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b38a2e9c-f034-4ce3-af1b-7d107f1c78df",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Multiple Conditions\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the `kind` is `'sporting'` and the `weight` is greater than 20.\n",
    "Answer:\n",
    "dogs[(dogs['kind'] == 'sporting') & (dogs['weight'] > 20)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9e5816af",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting        22589.0      12.92  medium   \n",
       "1            Cairn Terrier   terrier        21992.0      13.84   small   \n",
       "2   English Cocker Spaniel  sporting        18993.0      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working        13936.0       7.57   large   \n",
       "41                 Mastiff   working        13581.0       6.50   large   \n",
       "42           Saint Bernard   working        20022.0       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You'll see the Path(...) / subpath syntax a lot.\n",
    "# It creates the correct path to your file, \n",
    "# whether you're using Windows, macOS, or Linux.\n",
    "dog_path = Path('data') / 'dogs43.csv'\n",
    "dogs = pd.read_csv(dog_path)\n",
    "dogs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37dfa85f-ec32-4e80-96c8-44a7e4648bb5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Review: `head`, `tail`, `shape`, `index`, `get`, and `sort_values`\n",
    "\n",
    "To extract the first or last few rows of a DataFrame, use the `head` or `tail` methods."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c350c5c9-883b-4a6f-8c2f-237c6e65cb65",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Index\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the breed is 'Beagle'.\n",
    "\n",
    "Answer:\n",
    "dogs.loc['Beagle']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "388ff41d-ce46-4988-8702-eb1a9f098140",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Index\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the breed is 'Beagle'.\n",
    "\n",
    "Answer:\n",
    "dogs.loc['Beagle']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "40b81a37-c457-43ca-97ac-ba41e6192800",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    breed      kind  lifetime_cost  longevity    size  weight  \\\n",
       "0                Brittany  sporting        22589.0      12.92  medium    35.0   \n",
       "1           Cairn Terrier   terrier        21992.0      13.84   small    14.0   \n",
       "2  English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "\n",
       "   height  \n",
       "0    19.0  \n",
       "1    10.0  \n",
       "2    16.0  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d78c23d2-ea08-4c39-9bfb-534705770e92",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            breed     kind  lifetime_cost  longevity   size  weight  height\n",
       "41        Mastiff  working        13581.0       6.50  large   175.0    30.0\n",
       "42  Saint Bernard  working        20022.0       7.78  large   155.0    26.5"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.tail(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7697d12-c931-4495-8c96-e4e0436ec6eb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "The `shape` attribute returns the DataFrame's number of rows and columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5f544554-8a1c-4068-988c-fb8299820900",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(43, 7)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2b485d34-8303-4ba0-bbbf-98bc23d2b3d5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=43, step=1)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The default index of a DataFrame is 0, 1, 2, 3, ...\n",
    "dogs.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62715aaa-e801-4e15-b46a-8741df56351d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "We know that we can use `.get()` to select out a column or multiple columns..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d0e86468",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                   Brittany\n",
       "1              Cairn Terrier\n",
       "2     English Cocker Spaniel\n",
       "               ...          \n",
       "40               Bullmastiff\n",
       "41                   Mastiff\n",
       "42             Saint Bernard\n",
       "Name: breed, Length: 43, dtype: object"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.get('breed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e48a51ed-7ea4-4c3f-9355-4ffaf354df84",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>longevity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>12.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>13.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>11.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>7.57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>6.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>7.78</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  longevity\n",
       "0                 Brittany  sporting      12.92\n",
       "1            Cairn Terrier   terrier      13.84\n",
       "2   English Cocker Spaniel  sporting      11.66\n",
       "..                     ...       ...        ...\n",
       "40             Bullmastiff   working       7.57\n",
       "41                 Mastiff   working       6.50\n",
       "42           Saint Bernard   working       7.78\n",
       "\n",
       "[43 rows x 3 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.get(['breed', 'kind', 'longevity'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34c2821a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Most people don't use `.get` in practice; we'll see the more common technique in a few slides."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2585a295",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "And lastly, remember that to sort by a column, use the `sort_values` method. Like most DataFrame and Series methods, `sort_values` returns a new DataFrame, and doesn't modify the original."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6495fe7e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Borzoi</td>\n",
       "      <td>hound</td>\n",
       "      <td>16176.0</td>\n",
       "      <td>9.08</td>\n",
       "      <td>large</td>\n",
       "      <td>82.5</td>\n",
       "      <td>28.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Newfoundland</td>\n",
       "      <td>working</td>\n",
       "      <td>19351.0</td>\n",
       "      <td>9.32</td>\n",
       "      <td>large</td>\n",
       "      <td>125.0</td>\n",
       "      <td>27.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Dandie Dinmont Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21633.0</td>\n",
       "      <td>12.17</td>\n",
       "      <td>small</td>\n",
       "      <td>21.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Maltese</td>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Chihuahua</td>\n",
       "      <td>toy</td>\n",
       "      <td>26250.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>small</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed     kind  lifetime_cost  longevity   size  weight  \\\n",
       "41                 Mastiff  working        13581.0       6.50  large   175.0   \n",
       "36                  Borzoi    hound        16176.0       9.08  large    82.5   \n",
       "34            Newfoundland  working        19351.0       9.32  large   125.0   \n",
       "..                     ...      ...            ...        ...    ...     ...   \n",
       "29  Dandie Dinmont Terrier  terrier        21633.0      12.17  small    21.0   \n",
       "14                 Maltese      toy        19084.0      12.25  small     5.0   \n",
       "8                Chihuahua      toy        26250.0      16.50  small     5.5   \n",
       "\n",
       "    height  \n",
       "41    30.0  \n",
       "36    28.0  \n",
       "34    27.0  \n",
       "..     ...  \n",
       "29     9.0  \n",
       "14     9.0  \n",
       "8      5.0  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Note that the index is no longer 0, 1, 2, ...!\n",
    "dogs.sort_values('height', ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "1dd6df12",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Borzoi</td>\n",
       "      <td>hound</td>\n",
       "      <td>16176.0</td>\n",
       "      <td>9.08</td>\n",
       "      <td>large</td>\n",
       "      <td>82.5</td>\n",
       "      <td>28.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Newfoundland</td>\n",
       "      <td>working</td>\n",
       "      <td>19351.0</td>\n",
       "      <td>9.32</td>\n",
       "      <td>large</td>\n",
       "      <td>125.0</td>\n",
       "      <td>27.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Maltese</td>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Dandie Dinmont Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21633.0</td>\n",
       "      <td>12.17</td>\n",
       "      <td>small</td>\n",
       "      <td>21.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Chihuahua</td>\n",
       "      <td>toy</td>\n",
       "      <td>26250.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>small</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed     kind  lifetime_cost  longevity   size  weight  \\\n",
       "41                 Mastiff  working        13581.0       6.50  large   175.0   \n",
       "36                  Borzoi    hound        16176.0       9.08  large    82.5   \n",
       "34            Newfoundland  working        19351.0       9.32  large   125.0   \n",
       "..                     ...      ...            ...        ...    ...     ...   \n",
       "14                 Maltese      toy        19084.0      12.25  small     5.0   \n",
       "29  Dandie Dinmont Terrier  terrier        21633.0      12.17  small    21.0   \n",
       "8                Chihuahua      toy        26250.0      16.50  small     5.5   \n",
       "\n",
       "    height  \n",
       "41    30.0  \n",
       "36    28.0  \n",
       "34    27.0  \n",
       "..     ...  \n",
       "14     9.0  \n",
       "29     9.0  \n",
       "8      5.0  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# This sorts by 'height', \n",
    "# then breaks ties by 'longevity'.\n",
    "# Note the difference in the last three rows between\n",
    "# this DataFrame and the one above.\n",
    "dogs.sort_values(['height', 'longevity'],\n",
    "                 ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afa670fc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Note that `dogs` is not the DataFrame above. To save our changes, we'd need to say something like `dogs = dogs.sort_values...`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "052d364a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting        22589.0      12.92  medium   \n",
       "1            Cairn Terrier   terrier        21992.0      13.84   small   \n",
       "2   English Cocker Spaniel  sporting        18993.0      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working        13936.0       7.57   large   \n",
       "41                 Mastiff   working        13581.0       6.50   large   \n",
       "42           Saint Bernard   working        20022.0       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e14a96f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Setting the index\n",
    "\n",
    "Think of each row's index as its **unique identifier** or **name**. Often, we like to set the index of a DataFrame to a unique identifier if we have one available. We can do so with the `set_index` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5e1dcdda",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.set_index('breed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9e7f7a2e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting        22589.0      12.92  medium   \n",
       "1            Cairn Terrier   terrier        21992.0      13.84   small   \n",
       "2   English Cocker Spaniel  sporting        18993.0      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working        13936.0       7.57   large   \n",
       "41                 Mastiff   working        13581.0       6.50   large   \n",
       "42           Saint Bernard   working        20022.0       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The above cell didn't involve an assignment statement,\n",
    "# so dogs was unchanged.\n",
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "febd1498",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# By reassigning dogs, our changes will persist.\n",
    "dogs = dogs.set_index('breed')\n",
    "dogs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebddecaf-ad1d-4fae-b529-a6655d291fb5",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Breed\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the breed is either 'Beagle' or 'Pug'.\n",
    "\n",
    "Answer:\n",
    "dogs[(dogs.index == 'Beagle') | (dogs.index == 'Pug')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f88d4034-2bb7-4584-ab87-f40d332027d4",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Selecting Rows Based on Breed\n",
    "\n",
    "Question:\n",
    "Write code that selects all rows from the `dogs` DataFrame where the breed is either 'Beagle' or 'Pug'.\n",
    "\n",
    "Answer:\n",
    "dogs[(dogs.index == 'Beagle') | (dogs.index == 'Pug')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "99147102",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(43, 6)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# There used to be 7 columns, but now there are only 6!\n",
    "dogs.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc48ad57-04c6-4b25-bf0c-16f9bbbbfb80",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "    <h3>Question 🤔 (Answer at `lec02-index`)</h3>\n",
    "        \n",
    "<b>Ask <a href=\"https://chat.openai.com/\">ChatGPT</a>:</b>\n",
    "    \n",
    "- To explain what happens if you have duplicate values in a column and use `set_index()` on it.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e7b71eb-51b6-48b1-882b-7da7938662c7",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Understanding set_index() with Duplicates\n",
    "\n",
    "Question:\n",
    "What happens if you have duplicate values in a column and use `set_index()` on it?\n",
    "Answer:\n",
    "If you use `set_index()` on a column with duplicate values, pandas will raise an error because the index must contain unique values.  Each index value must uniquely identify a row.  Attempting to create an index with duplicates violates this constraint."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "115d9b5c-4884-4d6b-8dcb-96e0bc4498b0",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### Understanding set_index() with Duplicates\n",
    "\n",
    "Question:\n",
    "What happens if you have duplicate values in a column and use `set_index()` on it?\n",
    "Answer:\n",
    "If you use `set_index()` on a column with duplicate values, pandas will raise an error because the index must contain unique values.  Each index value must uniquely identify a row.  Attempting to create an index with duplicates violates this constraint."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "104c7638",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### 💡 Pro-Tip: Displaying more rows/columns\n",
    "\n",
    "Sometimes, you just want `pandas` to display a lot of rows and columns. You can use this helper function to do that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ea33e966",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from IPython.display import display\n",
    "def display_df(df, rows=pd.options.display.max_rows, cols=pd.options.display.max_columns):\n",
    "    \"\"\"Displays n rows and cols from df.\"\"\"\n",
    "    with pd.option_context(\"display.max_rows\", rows,\n",
    "                           \"display.max_columns\", cols):\n",
    "        display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "020ff872",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Newfoundland</th>\n",
       "      <td>working</td>\n",
       "      <td>19351.0</td>\n",
       "      <td>9.32</td>\n",
       "      <td>large</td>\n",
       "      <td>125.0</td>\n",
       "      <td>27.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bloodhound</th>\n",
       "      <td>hound</td>\n",
       "      <td>13824.0</td>\n",
       "      <td>6.75</td>\n",
       "      <td>large</td>\n",
       "      <td>85.0</td>\n",
       "      <td>25.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Borzoi</th>\n",
       "      <td>hound</td>\n",
       "      <td>16176.0</td>\n",
       "      <td>9.08</td>\n",
       "      <td>large</td>\n",
       "      <td>82.5</td>\n",
       "      <td>28.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alaskan Malamute</th>\n",
       "      <td>working</td>\n",
       "      <td>21986.0</td>\n",
       "      <td>10.67</td>\n",
       "      <td>large</td>\n",
       "      <td>80.0</td>\n",
       "      <td>24.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rhodesian Ridgeback</th>\n",
       "      <td>hound</td>\n",
       "      <td>16530.0</td>\n",
       "      <td>9.10</td>\n",
       "      <td>large</td>\n",
       "      <td>77.5</td>\n",
       "      <td>25.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Giant Schnauzer</th>\n",
       "      <td>working</td>\n",
       "      <td>26686.0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>large</td>\n",
       "      <td>77.5</td>\n",
       "      <td>25.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Clumber Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18084.0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>medium</td>\n",
       "      <td>70.0</td>\n",
       "      <td>18.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chesapeake Bay Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>16697.0</td>\n",
       "      <td>9.48</td>\n",
       "      <td>large</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Irish Setter</th>\n",
       "      <td>sporting</td>\n",
       "      <td>20323.0</td>\n",
       "      <td>11.63</td>\n",
       "      <td>large</td>\n",
       "      <td>65.0</td>\n",
       "      <td>26.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>German Shorthaired Pointer</th>\n",
       "      <td>sporting</td>\n",
       "      <td>25842.0</td>\n",
       "      <td>11.46</td>\n",
       "      <td>large</td>\n",
       "      <td>62.5</td>\n",
       "      <td>24.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gordon Setter</th>\n",
       "      <td>sporting</td>\n",
       "      <td>19605.0</td>\n",
       "      <td>11.10</td>\n",
       "      <td>large</td>\n",
       "      <td>62.5</td>\n",
       "      <td>25.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bull Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>18490.0</td>\n",
       "      <td>10.21</td>\n",
       "      <td>medium</td>\n",
       "      <td>60.0</td>\n",
       "      <td>21.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Golden Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21447.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>60.0</td>\n",
       "      <td>22.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pointer</th>\n",
       "      <td>sporting</td>\n",
       "      <td>24445.0</td>\n",
       "      <td>12.42</td>\n",
       "      <td>large</td>\n",
       "      <td>59.5</td>\n",
       "      <td>25.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Afghan Hound</th>\n",
       "      <td>hound</td>\n",
       "      <td>24077.0</td>\n",
       "      <td>11.92</td>\n",
       "      <td>large</td>\n",
       "      <td>55.0</td>\n",
       "      <td>26.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Siberian Husky</th>\n",
       "      <td>working</td>\n",
       "      <td>22049.0</td>\n",
       "      <td>12.58</td>\n",
       "      <td>medium</td>\n",
       "      <td>47.5</td>\n",
       "      <td>21.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Springer Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21946.0</td>\n",
       "      <td>12.54</td>\n",
       "      <td>medium</td>\n",
       "      <td>45.0</td>\n",
       "      <td>19.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Kerry Blue Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>17240.0</td>\n",
       "      <td>9.40</td>\n",
       "      <td>medium</td>\n",
       "      <td>36.5</td>\n",
       "      <td>18.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Staffordshire Bull Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21650.0</td>\n",
       "      <td>12.05</td>\n",
       "      <td>medium</td>\n",
       "      <td>31.0</td>\n",
       "      <td>15.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pembroke Welsh Corgi</th>\n",
       "      <td>herding</td>\n",
       "      <td>23978.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>26.0</td>\n",
       "      <td>11.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>24330.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>small</td>\n",
       "      <td>25.0</td>\n",
       "      <td>14.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tibetan Terrier</th>\n",
       "      <td>non-sporting</td>\n",
       "      <td>20336.0</td>\n",
       "      <td>12.31</td>\n",
       "      <td>small</td>\n",
       "      <td>24.0</td>\n",
       "      <td>15.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Basenji</th>\n",
       "      <td>hound</td>\n",
       "      <td>22096.0</td>\n",
       "      <td>13.58</td>\n",
       "      <td>medium</td>\n",
       "      <td>23.0</td>\n",
       "      <td>16.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shetland Sheepdog</th>\n",
       "      <td>herding</td>\n",
       "      <td>21006.0</td>\n",
       "      <td>12.53</td>\n",
       "      <td>small</td>\n",
       "      <td>22.0</td>\n",
       "      <td>14.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dandie Dinmont Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21633.0</td>\n",
       "      <td>12.17</td>\n",
       "      <td>small</td>\n",
       "      <td>21.0</td>\n",
       "      <td>9.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Scottish Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>17525.0</td>\n",
       "      <td>10.69</td>\n",
       "      <td>small</td>\n",
       "      <td>20.0</td>\n",
       "      <td>10.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pug</th>\n",
       "      <td>toy</td>\n",
       "      <td>18527.0</td>\n",
       "      <td>11.00</td>\n",
       "      <td>medium</td>\n",
       "      <td>16.0</td>\n",
       "      <td>16.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miniature Schnauzer</th>\n",
       "      <td>terrier</td>\n",
       "      <td>20087.0</td>\n",
       "      <td>11.81</td>\n",
       "      <td>small</td>\n",
       "      <td>15.5</td>\n",
       "      <td>13.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cavalier King Charles Spaniel</th>\n",
       "      <td>toy</td>\n",
       "      <td>18639.0</td>\n",
       "      <td>11.29</td>\n",
       "      <td>small</td>\n",
       "      <td>15.5</td>\n",
       "      <td>12.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Lhasa Apso</th>\n",
       "      <td>non-sporting</td>\n",
       "      <td>22031.0</td>\n",
       "      <td>13.92</td>\n",
       "      <td>small</td>\n",
       "      <td>15.0</td>\n",
       "      <td>10.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shih Tzu</th>\n",
       "      <td>toy</td>\n",
       "      <td>21152.0</td>\n",
       "      <td>13.20</td>\n",
       "      <td>small</td>\n",
       "      <td>12.5</td>\n",
       "      <td>9.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tibetan Spaniel</th>\n",
       "      <td>non-sporting</td>\n",
       "      <td>25549.0</td>\n",
       "      <td>14.42</td>\n",
       "      <td>small</td>\n",
       "      <td>12.0</td>\n",
       "      <td>10.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Norfolk Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>24308.0</td>\n",
       "      <td>13.07</td>\n",
       "      <td>small</td>\n",
       "      <td>12.0</td>\n",
       "      <td>9.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Toy Spaniel</th>\n",
       "      <td>toy</td>\n",
       "      <td>17521.0</td>\n",
       "      <td>10.10</td>\n",
       "      <td>small</td>\n",
       "      <td>11.0</td>\n",
       "      <td>10.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chihuahua</th>\n",
       "      <td>toy</td>\n",
       "      <td>26250.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>small</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Maltese</th>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       kind  lifetime_cost  longevity    size  \\\n",
       "breed                                                                           \n",
       "Mastiff                             working        13581.0       6.50   large   \n",
       "Saint Bernard                       working        20022.0       7.78   large   \n",
       "Newfoundland                        working        19351.0       9.32   large   \n",
       "Bullmastiff                         working        13936.0       7.57   large   \n",
       "Bloodhound                            hound        13824.0       6.75   large   \n",
       "Borzoi                                hound        16176.0       9.08   large   \n",
       "Alaskan Malamute                    working        21986.0      10.67   large   \n",
       "Rhodesian Ridgeback                   hound        16530.0       9.10   large   \n",
       "Giant Schnauzer                     working        26686.0      10.00   large   \n",
       "Clumber Spaniel                    sporting        18084.0      10.00  medium   \n",
       "Labrador Retriever                 sporting        21299.0      12.04  medium   \n",
       "Chesapeake Bay Retriever           sporting        16697.0       9.48   large   \n",
       "Irish Setter                       sporting        20323.0      11.63   large   \n",
       "German Shorthaired Pointer         sporting        25842.0      11.46   large   \n",
       "Gordon Setter                      sporting        19605.0      11.10   large   \n",
       "Bull Terrier                        terrier        18490.0      10.21  medium   \n",
       "Golden Retriever                   sporting        21447.0      12.04  medium   \n",
       "Pointer                            sporting        24445.0      12.42   large   \n",
       "Afghan Hound                          hound        24077.0      11.92   large   \n",
       "Siberian Husky                      working        22049.0      12.58  medium   \n",
       "English Springer Spaniel           sporting        21946.0      12.54  medium   \n",
       "Kerry Blue Terrier                  terrier        17240.0       9.40  medium   \n",
       "Brittany                           sporting        22589.0      12.92  medium   \n",
       "Staffordshire Bull Terrier          terrier        21650.0      12.05  medium   \n",
       "English Cocker Spaniel             sporting        18993.0      11.66  medium   \n",
       "Pembroke Welsh Corgi                herding        23978.0      12.25   small   \n",
       "Cocker Spaniel                     sporting        24330.0      12.50   small   \n",
       "Tibetan Terrier                non-sporting        20336.0      12.31   small   \n",
       "Basenji                               hound        22096.0      13.58  medium   \n",
       "Shetland Sheepdog                   herding        21006.0      12.53   small   \n",
       "Dandie Dinmont Terrier              terrier        21633.0      12.17   small   \n",
       "Scottish Terrier                    terrier        17525.0      10.69   small   \n",
       "Pug                                     toy        18527.0      11.00  medium   \n",
       "Miniature Schnauzer                 terrier        20087.0      11.81   small   \n",
       "Cavalier King Charles Spaniel           toy        18639.0      11.29   small   \n",
       "Lhasa Apso                     non-sporting        22031.0      13.92   small   \n",
       "Cairn Terrier                       terrier        21992.0      13.84   small   \n",
       "Shih Tzu                                toy        21152.0      13.20   small   \n",
       "Tibetan Spaniel                non-sporting        25549.0      14.42   small   \n",
       "Norfolk Terrier                     terrier        24308.0      13.07   small   \n",
       "English Toy Spaniel                     toy        17521.0      10.10   small   \n",
       "Chihuahua                               toy        26250.0      16.50   small   \n",
       "Maltese                                 toy        19084.0      12.25   small   \n",
       "\n",
       "                               weight  height  \n",
       "breed                                          \n",
       "Mastiff                         175.0   30.00  \n",
       "Saint Bernard                   155.0   26.50  \n",
       "Newfoundland                    125.0   27.00  \n",
       "Bullmastiff                     115.0   25.50  \n",
       "Bloodhound                       85.0   25.00  \n",
       "Borzoi                           82.5   28.00  \n",
       "Alaskan Malamute                 80.0   24.00  \n",
       "Rhodesian Ridgeback              77.5   25.50  \n",
       "Giant Schnauzer                  77.5   25.50  \n",
       "Clumber Spaniel                  70.0   18.50  \n",
       "Labrador Retriever               67.5   23.00  \n",
       "Chesapeake Bay Retriever         67.5   23.50  \n",
       "Irish Setter                     65.0   26.00  \n",
       "German Shorthaired Pointer       62.5   24.00  \n",
       "Gordon Setter                    62.5   25.00  \n",
       "Bull Terrier                     60.0   21.50  \n",
       "Golden Retriever                 60.0   22.75  \n",
       "Pointer                          59.5   25.50  \n",
       "Afghan Hound                     55.0   26.00  \n",
       "Siberian Husky                   47.5   21.75  \n",
       "English Springer Spaniel         45.0   19.50  \n",
       "Kerry Blue Terrier               36.5   18.50  \n",
       "Brittany                         35.0   19.00  \n",
       "Staffordshire Bull Terrier       31.0   15.00  \n",
       "English Cocker Spaniel           30.0   16.00  \n",
       "Pembroke Welsh Corgi             26.0   11.00  \n",
       "Cocker Spaniel                   25.0   14.50  \n",
       "Tibetan Terrier                  24.0   15.50  \n",
       "Basenji                          23.0   16.50  \n",
       "Shetland Sheepdog                22.0   14.50  \n",
       "Dandie Dinmont Terrier           21.0    9.00  \n",
       "Scottish Terrier                 20.0   10.00  \n",
       "Pug                              16.0   16.00  \n",
       "Miniature Schnauzer              15.5   13.00  \n",
       "Cavalier King Charles Spaniel    15.5   12.50  \n",
       "Lhasa Apso                       15.0   10.50  \n",
       "Cairn Terrier                    14.0   10.00  \n",
       "Shih Tzu                         12.5    9.75  \n",
       "Tibetan Spaniel                  12.0   10.00  \n",
       "Norfolk Terrier                  12.0    9.50  \n",
       "English Toy Spaniel              11.0   10.00  \n",
       "Chihuahua                         5.5    5.00  \n",
       "Maltese                           5.0    9.00  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_df(dogs.sort_values('weight', ascending=False),\n",
    "           rows=43)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58124491",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Selecting columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c142aa3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Selecting columns in `babypandas` 👶🐼\n",
    "\n",
    "- In `babypandas`, you selected columns using the `.get` method.\n",
    "- `.get` also works in `pandas`, but it is not **idiomatic** – people don't usually use it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0b2c4566",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d7b67995",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed\n",
       "Brittany                  medium\n",
       "Cairn Terrier              small\n",
       "English Cocker Spaniel    medium\n",
       "                           ...  \n",
       "Bullmastiff                large\n",
       "Mastiff                    large\n",
       "Saint Bernard              large\n",
       "Name: size, Length: 43, dtype: object"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.get('size')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "9749741a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# This doesn't error, but sometimes we'd like it to.\n",
    "dogs.get('size oops!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2db466c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Selecting columns with `[]`\n",
    "\n",
    "* The standard way to select a column in `pandas` is by using the `[]` operator.\n",
    "* Specifying a column name returns the column as a Series.\n",
    "* Specifying a list of column names returns a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e5fe2c2a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "afdc913a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed\n",
       "Brittany                  sporting\n",
       "Cairn Terrier              terrier\n",
       "English Cocker Spaniel    sporting\n",
       "                            ...   \n",
       "Bullmastiff                working\n",
       "Mastiff                    working\n",
       "Saint Bernard              working\n",
       "Name: kind, Length: 43, dtype: object"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Returns a Series.\n",
    "dogs['kind']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "2cf38cf3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>size</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>small</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>large</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>large</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>large</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind    size\n",
       "breed                                   \n",
       "Brittany                sporting  medium\n",
       "Cairn Terrier            terrier   small\n",
       "English Cocker Spaniel  sporting  medium\n",
       "...                          ...     ...\n",
       "Bullmastiff              working   large\n",
       "Mastiff                  working   large\n",
       "Saint Bernard            working   large\n",
       "\n",
       "[43 rows x 2 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Returns a DataFrame.\n",
    "dogs[['kind', 'size']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "f33f6513",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind\n",
       "breed                           \n",
       "Brittany                sporting\n",
       "Cairn Terrier            terrier\n",
       "English Cocker Spaniel  sporting\n",
       "...                          ...\n",
       "Bullmastiff              working\n",
       "Mastiff                  working\n",
       "Saint Bernard            working\n",
       "\n",
       "[43 rows x 1 columns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 🤔\n",
    "dogs[['kind']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7d620b54",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [],
   "source": [
    "# Breeds are stored in the index, which is not a column!\n",
    "# dogs['breed']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "74ce6f66",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Brittany', 'Cairn Terrier', 'English Cocker Spaniel', 'Cocker Spaniel',\n",
       "       'Shetland Sheepdog', 'Siberian Husky', 'Lhasa Apso',\n",
       "       'Miniature Schnauzer', 'Chihuahua', 'English Springer Spaniel',\n",
       "       'German Shorthaired Pointer', 'Pointer', 'Tibetan Spaniel',\n",
       "       'Labrador Retriever', 'Maltese', 'Shih Tzu', 'Irish Setter',\n",
       "       'Golden Retriever', 'Chesapeake Bay Retriever', 'Tibetan Terrier',\n",
       "       'Gordon Setter', 'Pug', 'Norfolk Terrier', 'English Toy Spaniel',\n",
       "       'Cavalier King Charles Spaniel', 'Basenji',\n",
       "       'Staffordshire Bull Terrier', 'Pembroke Welsh Corgi', 'Clumber Spaniel',\n",
       "       'Dandie Dinmont Terrier', 'Giant Schnauzer', 'Scottish Terrier',\n",
       "       'Kerry Blue Terrier', 'Afghan Hound', 'Newfoundland',\n",
       "       'Rhodesian Ridgeback', 'Borzoi', 'Bull Terrier', 'Alaskan Malamute',\n",
       "       'Bloodhound', 'Bullmastiff', 'Mastiff', 'Saint Bernard'],\n",
       "      dtype='object', name='breed')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77e32b3c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Useful Series methods\n",
    "\n",
    "There are a variety of useful methods that work on Series. You can see the entire list [here](https://pandas.pydata.org/docs/reference/api/pandas.Series.html). Many methods that work on a Series will also work on DataFrames, as we'll soon see."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "501cd9eb-37c2-4669-b7bc-ebb9c5b5cadc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "014e7e53",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['sporting', 'terrier', 'herding', 'working', 'non-sporting', 'toy',\n",
       "       'hound'], dtype=object)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What are the unique kinds of dogs?\n",
    "dogs['kind'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "af60b15d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# How many unique kinds of dogs are there?\n",
    "dogs['kind'].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "660aef64",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "kind\n",
       "sporting        12\n",
       "terrier          8\n",
       "working          7\n",
       "toy              6\n",
       "hound            5\n",
       "non-sporting     3\n",
       "herding          2\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What's the distribution of kinds?\n",
    "dogs['kind'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d0da3249",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(11.340697674418605)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What's the mean of the 'longevity' column?\n",
    "dogs['longevity'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "93c6f30c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     43.00\n",
       "mean      49.35\n",
       "std       39.42\n",
       "          ...  \n",
       "50%       36.50\n",
       "75%       67.50\n",
       "max      175.00\n",
       "Name: weight, Length: 8, dtype: float64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Tell me more about the 'weight' column.\n",
    "dogs['weight'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "be6b5ebc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed\n",
       "Mastiff                       13581.0\n",
       "Bloodhound                    13824.0\n",
       "Bullmastiff                   13936.0\n",
       "                               ...   \n",
       "German Shorthaired Pointer    25842.0\n",
       "Chihuahua                     26250.0\n",
       "Giant Schnauzer               26686.0\n",
       "Name: lifetime_cost, Length: 43, dtype: float64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sort the 'lifetime_cost' column. Note that here we're using sort_values on a Series, not a DataFrame!\n",
    "dogs['lifetime_cost'].sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "03e5ca18",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Giant Schnauzer'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Gives us the index of the largest value, not the largest value itself.\n",
    "dogs['lifetime_cost'].idxmax()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7bd05aa",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Selecting subsets of rows (and columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35004e1e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Use `loc` to _slice_ rows and columns using labels\n",
    "\n",
    "You saw slicing in DSC 20.\n",
    "\n",
    "`loc` works similarly to slicing 2D arrays, but it uses row **labels** and column **labels**, not positions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c53204da",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "bd7d016d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(11.0)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The first argument is the row label.\n",
    "#        ↓\n",
    "dogs.loc['Pug', 'longevity']\n",
    "#                  ↑\n",
    "# The second argument is the column label."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f54795e0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "As an aside, `loc` is _not_ a method – it's an indexer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "f74c2c85",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexing._LocIndexer"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(dogs.loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "80c8461f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "method"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(dogs.sort_values)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "680f7e18-e49b-4763-a227-35503e029024",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `.loc` is flexible 🧘\n",
    "\n",
    "You can provide a sequence (list, array, Series) as either argument to `.loc`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "661fa35d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "a1c9b6c0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed\n",
       "Cocker Spaniel         small\n",
       "Labrador Retriever    medium\n",
       "Name: size, dtype: object"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.loc[['Cocker Spaniel', 'Labrador Retriever'], 'size']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "f88a77de-8073-4d46-8f0a-53fe0bdd6774",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>size</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>small</td>\n",
       "      <td>14.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>medium</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        kind    size  height\n",
       "breed                                       \n",
       "Cocker Spaniel      sporting   small    14.5\n",
       "Labrador Retriever  sporting  medium    23.0"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.loc[['Cocker Spaniel', 'Labrador Retriever'], ['kind', 'size', 'height']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "1fc39291",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "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>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>24330.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>small</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    lifetime_cost  longevity    size  weight\n",
       "breed                                                       \n",
       "Cocker Spaniel            24330.0      12.50   small    25.0\n",
       "Labrador Retriever        21299.0      12.04  medium    67.5"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Note that the 'weight' column is included!\n",
    "dogs.loc[['Cocker Spaniel', 'Labrador Retriever'], 'lifetime_cost': 'weight']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "33b823df-bf41-42e7-b0e8-f9f98a0ff59b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>24330.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>small</td>\n",
       "      <td>25.0</td>\n",
       "      <td>14.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        kind  lifetime_cost  longevity    size  weight  height\n",
       "breed                                                                         \n",
       "Cocker Spaniel      sporting        24330.0      12.50   small    25.0    14.5\n",
       "Labrador Retriever  sporting        21299.0      12.04  medium    67.5    23.0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.loc[['Cocker Spaniel', 'Labrador Retriever'], :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "2a9bb163-6cf1-42a2-994f-71b5dbd8567f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>24330.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>small</td>\n",
       "      <td>25.0</td>\n",
       "      <td>14.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        kind  lifetime_cost  longevity    size  weight  height\n",
       "breed                                                                         \n",
       "Cocker Spaniel      sporting        24330.0      12.50   small    25.0    14.5\n",
       "Labrador Retriever  sporting        21299.0      12.04  medium    67.5    23.0"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Shortcut for the line above.\n",
    "dogs.loc[['Cocker Spaniel', 'Labrador Retriever']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5adb344",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Review: Querying\n",
    "- As we saw in DSC 10, querying is the act of selecting rows in a DataFrame that satisfy certain condition(s).\n",
    "- Comparisons with arrays (or Series) result in Boolean arrays (or Series).\n",
    "- We can use comparisons along with the `loc` operator to **filter** a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "fe2f73f5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "7535d099",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Chihuahua</th>\n",
       "      <td>toy</td>\n",
       "      <td>26250.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>small</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Maltese</th>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          kind  lifetime_cost  longevity   size  weight  height\n",
       "breed                                                          \n",
       "Chihuahua  toy        26250.0      16.50  small     5.5     5.0\n",
       "Maltese    toy        19084.0      12.25  small     5.0     9.0"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.loc[dogs['weight'] < 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "40805c1e-7e16-4196-92ac-be89ef31d878",
   "metadata": {
    "editable": true,
    "scrolled": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Golden Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21447.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>60.0</td>\n",
       "      <td>22.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chesapeake Bay Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>16697.0</td>\n",
       "      <td>9.48</td>\n",
       "      <td>large</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                          \n",
       "Labrador Retriever        sporting        21299.0      12.04  medium    67.5   \n",
       "Golden Retriever          sporting        21447.0      12.04  medium    60.0   \n",
       "Chesapeake Bay Retriever  sporting        16697.0       9.48   large    67.5   \n",
       "\n",
       "                          height  \n",
       "breed                             \n",
       "Labrador Retriever         23.00  \n",
       "Golden Retriever           22.75  \n",
       "Chesapeake Bay Retriever   23.50  "
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.loc[dogs.index.str.contains('Retriever')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "9318edf4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Golden Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21447.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "      <td>60.0</td>\n",
       "      <td>22.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chesapeake Bay Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>16697.0</td>\n",
       "      <td>9.48</td>\n",
       "      <td>large</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                          \n",
       "Labrador Retriever        sporting        21299.0      12.04  medium    67.5   \n",
       "Golden Retriever          sporting        21447.0      12.04  medium    60.0   \n",
       "Chesapeake Bay Retriever  sporting        16697.0       9.48   large    67.5   \n",
       "\n",
       "                          height  \n",
       "breed                             \n",
       "Labrador Retriever         23.00  \n",
       "Golden Retriever           22.75  \n",
       "Chesapeake Bay Retriever   23.50  "
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Because querying is so common, there's a shortcut:\n",
    "dogs[dogs.index.str.contains('Retriever')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "79ce6b2a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [kind, lifetime_cost, longevity, size, weight, height]\n",
       "Index: []"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Empty DataFrame – not an error!\n",
    "dogs.loc[dogs['kind'] == 'beaver']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3161a69",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "Note that because we set the index to `'breed'` earlier, we can select rows based on dog breeds without having to query."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "6ad44407",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "436fa37d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "kind                 toy\n",
       "lifetime_cost    19084.0\n",
       "longevity          12.25\n",
       "size               small\n",
       "weight               5.0\n",
       "height               9.0\n",
       "Name: Maltese, dtype: object"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series!\n",
    "dogs.loc['Maltese']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46e8bf7a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "If `'breed'` was instead a column, then we'd need to query to access information about a particular breed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "b461d5f8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting        22589.0      12.92  medium   \n",
       "1            Cairn Terrier   terrier        21992.0      13.84   small   \n",
       "2   English Cocker Spaniel  sporting        18993.0      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working        13936.0       7.57   large   \n",
       "41                 Mastiff   working        13581.0       6.50   large   \n",
       "42           Saint Bernard   working        20022.0       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs_reset = dogs.reset_index()\n",
    "dogs_reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "2d0518f1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Maltese</td>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      breed kind  lifetime_cost  longevity   size  weight  height\n",
       "14  Maltese  toy        19084.0      12.25  small     5.0     9.0"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame!\n",
    "dogs_reset[dogs_reset['breed'] == 'Maltese']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632b8d8f-c1a8-4b0a-b9f0-2404fc1ca7a4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Querying with multiple conditions\n",
    "\n",
    "Remember, you need parentheses around each condition. Also, you must use the bitwise operators `&` and `|` instead of the standard `and` and `or` keywords. `pandas` makes weird decisions sometimes!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "dce9f7db",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "32c7311f-5c9e-45a0-9600-619e944ca9fb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miniature Schnauzer</th>\n",
       "      <td>terrier</td>\n",
       "      <td>20087.0</td>\n",
       "      <td>11.81</td>\n",
       "      <td>small</td>\n",
       "      <td>15.5</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Norfolk Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>24308.0</td>\n",
       "      <td>13.07</td>\n",
       "      <td>small</td>\n",
       "      <td>12.0</td>\n",
       "      <td>9.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        kind  lifetime_cost  longevity   size  weight  height\n",
       "breed                                                                        \n",
       "Cairn Terrier        terrier        21992.0      13.84  small    14.0    10.0\n",
       "Miniature Schnauzer  terrier        20087.0      11.81  small    15.5    13.0\n",
       "Norfolk Terrier      terrier        24308.0      13.07  small    12.0     9.5"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs[(dogs['weight'] < 20) & (dogs['kind'] == 'terrier')]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c6feca9-68f0-41c6-8648-bedfe04599ca",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### 💡 Pro-Tip: Using `.query`\n",
    "\n",
    "`.query` is a convenient way to query, since you don't need parentheses and you can use the `and` and `or` keywords."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "d15b416a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "56f7e9f3-f16f-41e4-9360-e69e48eaeb53",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miniature Schnauzer</th>\n",
       "      <td>terrier</td>\n",
       "      <td>20087.0</td>\n",
       "      <td>11.81</td>\n",
       "      <td>small</td>\n",
       "      <td>15.5</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Norfolk Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>24308.0</td>\n",
       "      <td>13.07</td>\n",
       "      <td>small</td>\n",
       "      <td>12.0</td>\n",
       "      <td>9.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        kind  lifetime_cost  longevity   size  weight  height\n",
       "breed                                                                        \n",
       "Cairn Terrier        terrier        21992.0      13.84  small    14.0    10.0\n",
       "Miniature Schnauzer  terrier        20087.0      11.81  small    15.5    13.0\n",
       "Norfolk Terrier      terrier        24308.0      13.07  small    12.0     9.5"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.query('weight < 20 and kind == \"terrier\"')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "72d9521a-a71c-4b54-900c-50169fb447de",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chesapeake Bay Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>16697.0</td>\n",
       "      <td>9.48</td>\n",
       "      <td>large</td>\n",
       "      <td>67.5</td>\n",
       "      <td>23.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gordon Setter</th>\n",
       "      <td>sporting</td>\n",
       "      <td>19605.0</td>\n",
       "      <td>11.10</td>\n",
       "      <td>large</td>\n",
       "      <td>62.5</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Clumber Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18084.0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>medium</td>\n",
       "      <td>70.0</td>\n",
       "      <td>18.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Scottish Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>17525.0</td>\n",
       "      <td>10.69</td>\n",
       "      <td>small</td>\n",
       "      <td>20.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Kerry Blue Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>17240.0</td>\n",
       "      <td>9.40</td>\n",
       "      <td>medium</td>\n",
       "      <td>36.5</td>\n",
       "      <td>18.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bull Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>18490.0</td>\n",
       "      <td>10.21</td>\n",
       "      <td>medium</td>\n",
       "      <td>60.0</td>\n",
       "      <td>21.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                          \n",
       "English Cocker Spaniel    sporting        18993.0      11.66  medium    30.0   \n",
       "Chesapeake Bay Retriever  sporting        16697.0       9.48   large    67.5   \n",
       "Gordon Setter             sporting        19605.0      11.10   large    62.5   \n",
       "Clumber Spaniel           sporting        18084.0      10.00  medium    70.0   \n",
       "Scottish Terrier           terrier        17525.0      10.69   small    20.0   \n",
       "Kerry Blue Terrier         terrier        17240.0       9.40  medium    36.5   \n",
       "Bull Terrier               terrier        18490.0      10.21  medium    60.0   \n",
       "\n",
       "                          height  \n",
       "breed                             \n",
       "English Cocker Spaniel      16.0  \n",
       "Chesapeake Bay Retriever    23.5  \n",
       "Gordon Setter               25.0  \n",
       "Clumber Spaniel             18.5  \n",
       "Scottish Terrier            10.0  \n",
       "Kerry Blue Terrier          18.5  \n",
       "Bull Terrier                21.5  "
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.query('kind in [\"sporting\", \"terrier\"] and lifetime_cost < 20000')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d18cfac-8767-45a9-8aed-565b32aec51b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "    <h3>Question 🤔 (Answer at `lec02-query`)</h3>\n",
    "        \n",
    "<b>Ask <a href=\"https://chat.openai.com/\">ChatGPT</a>:</b>\n",
    "    \n",
    "- To explain when you would use `.query()` instead of `.loc[]` or the other way around.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e1a3a89",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Don't forget `iloc`!\n",
    "\n",
    "- `iloc` stands for \"integer location.\"\n",
    "- `iloc` is like `loc`, but it selects rows and columns based off of integer positions only, just like with 2D arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2e3b6051",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "dd494f9f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>24330.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>small</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>Tibetan Spaniel</th>\n",
       "      <td>non-sporting</td>\n",
       "      <td>25549.0</td>\n",
       "      <td>14.42</td>\n",
       "      <td>small</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Labrador Retriever</th>\n",
       "      <td>sporting</td>\n",
       "      <td>21299.0</td>\n",
       "      <td>12.04</td>\n",
       "      <td>medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Maltese</th>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                kind  lifetime_cost  longevity    size\n",
       "breed                                                                 \n",
       "Cairn Terrier                terrier        21992.0      13.84   small\n",
       "English Cocker Spaniel      sporting        18993.0      11.66  medium\n",
       "Cocker Spaniel              sporting        24330.0      12.50   small\n",
       "...                              ...            ...        ...     ...\n",
       "Tibetan Spaniel         non-sporting        25549.0      14.42   small\n",
       "Labrador Retriever          sporting        21299.0      12.04  medium\n",
       "Maltese                          toy        19084.0      12.25   small\n",
       "\n",
       "[14 rows x 4 columns]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.iloc[1:15, :-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53425cfc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "`iloc` is often most useful when we sort first. For instance, to find the weight of the longest-living dog breed in the dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "d5c69f04-1e70-4f0c-a355-a4642019efe0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(5.5)"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.sort_values('longevity', ascending=False)['weight'].iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "1878d0e5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Chihuahua'"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Finding the breed itself involves sorting, but not iloc.\n",
    "dogs.sort_values('longevity', ascending=False).index[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e0f6afd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### More practice\n",
    "\n",
    "Consider the DataFrame below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "84d1dd06",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>1</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>fee</td>\n",
       "      <td>fo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>fi</td>\n",
       "      <td>fum</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     1    1\n",
       "0  fee   fo\n",
       "1   fi  fum"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jack = pd.DataFrame({1: ['fee', 'fi'], \n",
    "                     '1': ['fo', 'fum']})\n",
    "jack"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "420f47d8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "For each of the following pieces of code, predict what the output will be. Then, uncomment the line of code and see for yourself. We may not be able to cover these all in class; if so, make sure to try them on your own. [Here's a Pandas Tutor link](https://pandastutor.com/vis.html#code=import%20pandas%20as%20pd%0A%0Ajack%20%3D%20pd.DataFrame%28%7B1%3A%20%5B'fee',%20'fi'%5D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'1'%3A%20%5B'fo',%20'fum'%5D%7D%29%0Ajack%5B1%5D&d=2023-10-05&lang=py&v=v1) to visualize these!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "660fa42d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "ac5ad541",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack[[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "0c1f7c00",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack['1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "24a90b6b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack[[1, 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "bda6e0f9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack.loc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "bb96863b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack.loc[jack[1] == 'fo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "b83cc118",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack[1, ['1', 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "4a157e2f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# jack.loc[1,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a168d5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "    <h3>Questions? 🤔</h3>\n",
    "</div>\n",
    "    \n",
    "What questions do you have?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d58ea98",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "    <h3>We will probably have to end lecture here.</h3>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eaeba51-a0c3-4ed7-b762-2d1bcae3162a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Adding and modifying columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48b0b604-3923-4d8d-a2ee-72c756f61fda",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Adding and modifying columns, using a copy\n",
    "\n",
    "- To add a new column to a DataFrame, use the `assign` method.\n",
    "    - To change the values in a column, add a new column with the same name as the existing column.\n",
    "- Like most `pandas` methods, `assign` returns a new DataFrame.\n",
    "    - **Pro** ✅: This doesn't inadvertently change any existing variables.\n",
    "    - **Con** ❌: It is not very space efficient, as it creates a new copy each time it is called."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "c965145f-ae2a-4e19-a1c5-ca828c479883",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost_per_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1748.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1628.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "      <td>1840.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2089.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "      <td>2573.52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  cost_per_year  \n",
       "breed                                          \n",
       "Brittany                  19.0        1748.37  \n",
       "Cairn Terrier             10.0        1589.02  \n",
       "English Cocker Spaniel    16.0        1628.90  \n",
       "...                        ...            ...  \n",
       "Bullmastiff               25.5        1840.95  \n",
       "Mastiff                   30.0        2089.38  \n",
       "Saint Bernard             26.5        2573.52  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.assign(cost_per_year=dogs['lifetime_cost'] / dogs['longevity'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "b6967445",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e4fc26b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### 💡 Pro-Tip: Method chaining\n",
    "\n",
    "Chain methods together instead of writing long, hard-to-read lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "9e83ee9b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost_per_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Maltese</th>\n",
       "      <td>toy</td>\n",
       "      <td>19084.0</td>\n",
       "      <td>12.25</td>\n",
       "      <td>small</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.00</td>\n",
       "      <td>1557.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Lhasa Apso</th>\n",
       "      <td>non-sporting</td>\n",
       "      <td>22031.0</td>\n",
       "      <td>13.92</td>\n",
       "      <td>small</td>\n",
       "      <td>15.0</td>\n",
       "      <td>10.50</td>\n",
       "      <td>1582.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chihuahua</th>\n",
       "      <td>toy</td>\n",
       "      <td>26250.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>small</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.00</td>\n",
       "      <td>1590.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shih Tzu</th>\n",
       "      <td>toy</td>\n",
       "      <td>21152.0</td>\n",
       "      <td>13.20</td>\n",
       "      <td>small</td>\n",
       "      <td>12.5</td>\n",
       "      <td>9.75</td>\n",
       "      <td>1602.42</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       kind  lifetime_cost  longevity   size  weight  height  \\\n",
       "breed                                                                          \n",
       "Maltese                 toy        19084.0      12.25  small     5.0    9.00   \n",
       "Lhasa Apso     non-sporting        22031.0      13.92  small    15.0   10.50   \n",
       "Cairn Terrier       terrier        21992.0      13.84  small    14.0   10.00   \n",
       "Chihuahua               toy        26250.0      16.50  small     5.5    5.00   \n",
       "Shih Tzu                toy        21152.0      13.20  small    12.5    9.75   \n",
       "\n",
       "               cost_per_year  \n",
       "breed                         \n",
       "Maltese              1557.88  \n",
       "Lhasa Apso           1582.69  \n",
       "Cairn Terrier        1589.02  \n",
       "Chihuahua            1590.91  \n",
       "Shih Tzu             1602.42  "
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Finds the rows corresponding to the five cheapest to own breeds on a per-year basis.\n",
    "(dogs\n",
    " .assign(cost_per_year=dogs['lifetime_cost'] / dogs['longevity'])\n",
    " .sort_values('cost_per_year')\n",
    " .iloc[:5]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d26fe50",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### 💡 Pro-Tip: `assign` for column names with special characters\n",
    "\n",
    "You can also use `assign` when the desired column name has spaces (and other special characters) by unpacking a dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "b992959a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost per year 💵</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1748.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1628.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "      <td>1840.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2089.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "      <td>2573.52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  cost per year 💵  \n",
       "breed                                            \n",
       "Brittany                  19.0          1748.37  \n",
       "Cairn Terrier             10.0          1589.02  \n",
       "English Cocker Spaniel    16.0          1628.90  \n",
       "...                        ...              ...  \n",
       "Bullmastiff               25.5          1840.95  \n",
       "Mastiff                   30.0          2089.38  \n",
       "Saint Bernard             26.5          2573.52  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.assign(**{'cost per year 💵': dogs['lifetime_cost'] / dogs['longevity']})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb6fa478",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Adding and modifying columns, in-place\n",
    "\n",
    "* You can assign a new column to a DataFrame **in-place** using `[]`.\n",
    "    - This works like dictionary assignment.\n",
    "    - This **modifies** the underlying DataFrame, unlike `assign`, which returns a new DataFrame.\n",
    "* This is the more \"common\" way of adding/modifying columns. \n",
    "    - ⚠️ Warning: Exercise caution when using this approach, since this approach changes the values of existing variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "f598df4c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   kind  lifetime_cost  longevity    size  weight  height\n",
       "breed                                                                    \n",
       "Brittany       sporting        22589.0      12.92  medium    35.0    19.0\n",
       "Cairn Terrier   terrier        21992.0      13.84   small    14.0    10.0"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# By default, .copy() returns a deep copy of the object it is called on,\n",
    "# meaning that if you change the copy the original remains unmodified.\n",
    "dogs_copy = dogs.copy()\n",
    "dogs_copy.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "ba1f0726",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost_per_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1748.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1628.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "      <td>1840.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2089.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "      <td>2573.52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  cost_per_year  \n",
       "breed                                          \n",
       "Brittany                  19.0        1748.37  \n",
       "Cairn Terrier             10.0        1589.02  \n",
       "English Cocker Spaniel    16.0        1628.90  \n",
       "...                        ...            ...  \n",
       "Bullmastiff               25.5        1840.95  \n",
       "Mastiff                   30.0        2089.38  \n",
       "Saint Bernard             26.5        2573.52  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs_copy['cost_per_year'] = dogs_copy['lifetime_cost'] / dogs_copy['longevity']\n",
    "dogs_copy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b5d2ac1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "Note that we never reassigned `dogs_copy` in the cell above – that is, we never wrote `dogs_copy = ...` – though it was still modified."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76a86690",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Mutability\n",
    "\n",
    "DataFrames, like lists, arrays, and dictionaries, are **mutable**. As you learned in DSC 20, this means that they can be modified after being created. (For instance, the list `.append` method mutates in-place.)\n",
    "\n",
    "Not only does this explain the behavior on the previous slide, but it also explains the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "574b4b11",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost_per_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1748.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1628.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "      <td>1840.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2089.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "      <td>2573.52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  cost_per_year  \n",
       "breed                                          \n",
       "Brittany                  19.0        1748.37  \n",
       "Cairn Terrier             10.0        1589.02  \n",
       "English Cocker Spaniel    16.0        1628.90  \n",
       "...                        ...            ...  \n",
       "Bullmastiff               25.5        1840.95  \n",
       "Mastiff                   30.0        2089.38  \n",
       "Saint Bernard             26.5        2573.52  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "fe506f4a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def cost_in_thousands():\n",
    "    dogs_copy['lifetime_cost'] = dogs_copy['lifetime_cost'] / 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "eb1be3f8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# What happens when we run this twice?\n",
    "cost_in_thousands()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "b3b53db2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "      <th>cost_per_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22.59</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>1748.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21.99</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1589.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18.99</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1628.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13.94</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "      <td>1840.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13.58</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>2089.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20.02</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "      <td>2573.52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting          22.59      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier          21.99      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting          18.99      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working          13.94       7.57   large   115.0   \n",
       "Mastiff                  working          13.58       6.50   large   175.0   \n",
       "Saint Bernard            working          20.02       7.78   large   155.0   \n",
       "\n",
       "                        height  cost_per_year  \n",
       "breed                                          \n",
       "Brittany                  19.0        1748.37  \n",
       "Cairn Terrier             10.0        1589.02  \n",
       "English Cocker Spaniel    16.0        1628.90  \n",
       "...                        ...            ...  \n",
       "Bullmastiff               25.5        1840.95  \n",
       "Mastiff                   30.0        2089.38  \n",
       "Saint Bernard             26.5        2573.52  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs_copy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6cbd332",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### ⚠️ Avoid mutation when possible\n",
    "\n",
    "Note that `dogs_copy` was modified, even though we didn't reassign it! These unintended consequences can **influence the behavior of test cases on labs and projects**, among other things! \n",
    "\n",
    "To avoid this, it's a good idea to avoid mutation when possible. If you must use mutation, include `df = df.copy()` as the first line in functions that take DataFrames as input.\n",
    "\n",
    "Also, some methods let you use the `inplace=True` argument to mutate the original. **Don't use this argument, since future `pandas` releases plan to remove it.**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5549d72e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## `pandas` and `numpy`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "099f532e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<center><img src='imgs/python-stack.png' width=60%></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d5900bf",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `pandas` is built upon `numpy`!\n",
    "\n",
    "- A Series in `pandas` is a `numpy` array with an index.\n",
    "- A DataFrame is like a dictionary of columns, each of which is a `numpy` array.\n",
    "- Many operations in `pandas` are fast because they use `numpy`'s implementations, which are written in fast languages like C.\n",
    "- If you need access the array underlying a DataFrame or Series, use the `to_numpy` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "da955778",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed\n",
       "Brittany                  22589.0\n",
       "Cairn Terrier             21992.0\n",
       "English Cocker Spaniel    18993.0\n",
       "                           ...   \n",
       "Bullmastiff               13936.0\n",
       "Mastiff                   13581.0\n",
       "Saint Bernard             20022.0\n",
       "Name: lifetime_cost, Length: 43, dtype: float64"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs['lifetime_cost']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "91195593",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([22589., 21992., 18993., ..., 13936., 13581., 20022.])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs['lifetime_cost'].to_numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1c6a1f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `pandas` data types\n",
    "\n",
    "- Each Series (column) has a `numpy` data type, which refers to the type of the values stored within. Access it using the `dtypes` attribute.\n",
    "- A column's data type determines which operations can be applied to it.\n",
    "- `pandas` tries to guess the correct data types for a given DataFrame, and is often wrong.\n",
    "    - This can lead to incorrect calculations and poor memory/time performance.\n",
    "- As a result, you will often need to explicitly convert between data types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "6372e856",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "dd039187",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "kind              object\n",
       "lifetime_cost    float64\n",
       "longevity        float64\n",
       "size              object\n",
       "weight           float64\n",
       "height           float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2484929",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### `pandas` data types\n",
    "\n",
    "Notice that Python `str` types are `object` types in `numpy` and `pandas`.\n",
    "\n",
    "|Pandas dtype|Python type|NumPy type|SQL type|Usage|\n",
    "|---|---|---|---|---|\n",
    "|int64|int|int_, int8,...,int64, uint8,...,uint64|INT, BIGINT| Integer numbers|\n",
    "|float64|float|float_, float16, float32, float64|FLOAT| Floating point numbers|\n",
    "|bool|bool|bool_|BOOL|True/False values|\n",
    "|datetime64 or Timestamp|datetime.datetime|datetime64|DATETIME|Date and time values|\n",
    "|timedelta64 or Timedelta|datetime.timedelta|timedelta64|NA|Differences between two datetimes|\n",
    "|category|NA|NA|ENUM|Finite list of text values|\n",
    "|object|str|string, unicode|NA|Text|\n",
    "|object|NA|object|NA|Mixed types|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93ca8db1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "[This article](https://www.dataquest.io/blog/pandas-big-data/) details how `pandas` stores different data types under the hood.\n",
    "\n",
    "[This article](https://mortada.net/can-integer-operations-overflow-in-python.html#Can-integers-overflow-in-python?) explains how `numpy`/`pandas` `int64` operations differ from vanilla `int` operations."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea361cc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Type conversion\n",
    "\n",
    "You can change the data type of a Series using the `.astype` Series method.\n",
    "\n",
    "For example, we can change the data type of the `'lifetime_cost'` column in `dogs` to be `uint32`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "4309975a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>breed</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Brittany</th>\n",
       "      <td>sporting</td>\n",
       "      <td>22589.0</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cairn Terrier</th>\n",
       "      <td>terrier</td>\n",
       "      <td>21992.0</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>English Cocker Spaniel</th>\n",
       "      <td>sporting</td>\n",
       "      <td>18993.0</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bullmastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13936.0</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mastiff</th>\n",
       "      <td>working</td>\n",
       "      <td>13581.0</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Saint Bernard</th>\n",
       "      <td>working</td>\n",
       "      <td>20022.0</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            kind  lifetime_cost  longevity    size  weight  \\\n",
       "breed                                                                        \n",
       "Brittany                sporting        22589.0      12.92  medium    35.0   \n",
       "Cairn Terrier            terrier        21992.0      13.84   small    14.0   \n",
       "English Cocker Spaniel  sporting        18993.0      11.66  medium    30.0   \n",
       "...                          ...            ...        ...     ...     ...   \n",
       "Bullmastiff              working        13936.0       7.57   large   115.0   \n",
       "Mastiff                  working        13581.0       6.50   large   175.0   \n",
       "Saint Bernard            working        20022.0       7.78   large   155.0   \n",
       "\n",
       "                        height  \n",
       "breed                           \n",
       "Brittany                  19.0  \n",
       "Cairn Terrier             10.0  \n",
       "English Cocker Spaniel    16.0  \n",
       "...                        ...  \n",
       "Bullmastiff               25.5  \n",
       "Mastiff                   30.0  \n",
       "Saint Bernard             26.5  \n",
       "\n",
       "[43 rows x 6 columns]"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "22c66798",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 43 entries, Brittany to Saint Bernard\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Non-Null Count  Dtype  \n",
      "---  ------         --------------  -----  \n",
      " 0   kind           43 non-null     object \n",
      " 1   lifetime_cost  43 non-null     float64\n",
      " 2   longevity      43 non-null     float64\n",
      " 3   size           43 non-null     object \n",
      " 4   weight         43 non-null     float64\n",
      " 5   height         43 non-null     float64\n",
      "dtypes: float64(4), object(2)\n",
      "memory usage: 3.4+ KB\n"
     ]
    }
   ],
   "source": [
    "# Gives the types as well as the space taken up by the DataFrame.\n",
    "dogs.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "fb1ce8c9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "dogs['lifetime_cost'] = dogs['lifetime_cost'].astype('uint32')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87405f1b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Now, the DataFrame takes up less space! This may be insignificant in our DataFrame, but makes a difference when working with larger datasets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "1581d5ea",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 43 entries, Brittany to Saint Bernard\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Non-Null Count  Dtype  \n",
      "---  ------         --------------  -----  \n",
      " 0   kind           43 non-null     object \n",
      " 1   lifetime_cost  43 non-null     uint32 \n",
      " 2   longevity      43 non-null     float64\n",
      " 3   size           43 non-null     object \n",
      " 4   weight         43 non-null     float64\n",
      " 5   height         43 non-null     float64\n",
      "dtypes: float64(3), object(2), uint32(1)\n",
      "memory usage: 3.2+ KB\n"
     ]
    }
   ],
   "source": [
    "dogs.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c9de4e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### 💡 Pro-Tip: Setting `dtype`s in `read_csv`\n",
    "\n",
    "Usually, we prefer to set the correct dtypes in `read_csv`, since it can help `pandas` load in files more quickly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "65faf7f6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('data/dogs43.csv')"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dog_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "986d34c2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting          22589      12.92  medium   \n",
       "1            Cairn Terrier   terrier          21992      13.84   small   \n",
       "2   English Cocker Spaniel  sporting          18993      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working          13936       7.57   large   \n",
       "41                 Mastiff   working          13581       6.50   large   \n",
       "42           Saint Bernard   working          20022       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs = pd.read_csv(dog_path, dtype={'lifetime_cost': 'uint32'})\n",
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "d8ae92f0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed             object\n",
       "kind              object\n",
       "lifetime_cost     uint32\n",
       "longevity        float64\n",
       "size              object\n",
       "weight           float64\n",
       "height           float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fffa5177",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Axes\n",
    "\n",
    "- The rows and columns of a DataFrame are both stored as Series.\n",
    "- The **axis** specifies the direction of a **slice** of a DataFrame.\n",
    "\n",
    "<center><img src='imgs/axis.png' width=30%></center>\n",
    "\n",
    "- Axis 0 refers to the index (rows).\n",
    "- Axis 1 refers to the columns.\n",
    "- **These are the same axes definitions that 2D `numpy` arrays have!**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e91a09c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### DataFrame methods with `axis`\n",
    "\n",
    "- Many Series methods work on DataFrames.\n",
    "- In such cases, the DataFrame method usually applies the Series method to every row or column.\n",
    "- Many of these methods accept an `axis` argument; the default is usually `axis=0`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "0e674deb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>kind</th>\n",
       "      <th>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>22589</td>\n",
       "      <td>12.92</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Cairn Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>21992</td>\n",
       "      <td>13.84</td>\n",
       "      <td>small</td>\n",
       "      <td>14.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>English Cocker Spaniel</td>\n",
       "      <td>sporting</td>\n",
       "      <td>18993</td>\n",
       "      <td>11.66</td>\n",
       "      <td>medium</td>\n",
       "      <td>30.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Bullmastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13936</td>\n",
       "      <td>7.57</td>\n",
       "      <td>large</td>\n",
       "      <td>115.0</td>\n",
       "      <td>25.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>Mastiff</td>\n",
       "      <td>working</td>\n",
       "      <td>13581</td>\n",
       "      <td>6.50</td>\n",
       "      <td>large</td>\n",
       "      <td>175.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Saint Bernard</td>\n",
       "      <td>working</td>\n",
       "      <td>20022</td>\n",
       "      <td>7.78</td>\n",
       "      <td>large</td>\n",
       "      <td>155.0</td>\n",
       "      <td>26.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     breed      kind  lifetime_cost  longevity    size  \\\n",
       "0                 Brittany  sporting          22589      12.92  medium   \n",
       "1            Cairn Terrier   terrier          21992      13.84   small   \n",
       "2   English Cocker Spaniel  sporting          18993      11.66  medium   \n",
       "..                     ...       ...            ...        ...     ...   \n",
       "40             Bullmastiff   working          13936       7.57   large   \n",
       "41                 Mastiff   working          13581       6.50   large   \n",
       "42           Saint Bernard   working          20022       7.78   large   \n",
       "\n",
       "    weight  height  \n",
       "0     35.0    19.0  \n",
       "1     14.0    10.0  \n",
       "2     30.0    16.0  \n",
       "..     ...     ...  \n",
       "40   115.0    25.5  \n",
       "41   175.0    30.0  \n",
       "42   155.0    26.5  \n",
       "\n",
       "[43 rows x 7 columns]"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "89854475",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed            Tibetan Terrier\n",
       "kind                     working\n",
       "lifetime_cost              26686\n",
       "longevity                   16.5\n",
       "size                       small\n",
       "weight                     175.0\n",
       "height                      30.0\n",
       "dtype: object"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Max element in each column.\n",
    "dogs.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "01f5af45",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Max element in each row – throws an error since there are different types in each row.\n",
    "# dogs.max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "9a6231eb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "breed            43\n",
       "kind              7\n",
       "lifetime_cost    43\n",
       "longevity        40\n",
       "size              3\n",
       "weight           37\n",
       "height           30\n",
       "dtype: int64"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The number of unique values in each column.\n",
    "dogs.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "942d387e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>lifetime_cost</th>\n",
       "      <th>longevity</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>43.00</td>\n",
       "      <td>43.00</td>\n",
       "      <td>43.00</td>\n",
       "      <td>43.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>20532.84</td>\n",
       "      <td>11.34</td>\n",
       "      <td>49.35</td>\n",
       "      <td>18.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>3290.78</td>\n",
       "      <td>2.05</td>\n",
       "      <td>39.42</td>\n",
       "      <td>6.83</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>50%</th>\n",
       "      <td>21006.00</td>\n",
       "      <td>11.81</td>\n",
       "      <td>36.50</td>\n",
       "      <td>18.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>22072.50</td>\n",
       "      <td>12.52</td>\n",
       "      <td>67.50</td>\n",
       "      <td>25.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>26686.00</td>\n",
       "      <td>16.50</td>\n",
       "      <td>175.00</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       lifetime_cost  longevity  weight  height\n",
       "count          43.00      43.00   43.00   43.00\n",
       "mean        20532.84      11.34   49.35   18.34\n",
       "std          3290.78       2.05   39.42    6.83\n",
       "...              ...        ...     ...     ...\n",
       "50%         21006.00      11.81   36.50   18.50\n",
       "75%         22072.50      12.52   67.50   25.00\n",
       "max         26686.00      16.50  175.00   30.00\n",
       "\n",
       "[8 rows x 4 columns]"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# describe doesn't accept an axis argument; it works on every numeric column in the DataFrame it is called on.\n",
    "dogs.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d52afb2d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<h3>Exercise</h3>\n",
    "Pick a dog breed that you personally like or know the name of. Then:\n",
    "<ul>\n",
    "    <li>Try to find a few other dog breeds that are similar in weight to yours in <code>all_dogs</code>.</li>\n",
    "    <li>Which similar breeds have the lowest and highest <code>'lifetime_cost'</code>? <code>'intelligence_rank'</code>?</li>\n",
    "    <li>Are there any similar breeds that you haven't heard of before?</li>\n",
    "</ul>\n",
    "<br>\n",
    "    For fun, look up these dog breeds on the <a href=\"https://www.akc.org/\">AKC website</a> to see what they look like!\n",
    "</div>\n",
    "\n",
    "<!-- As a bonus, look up these dog breeds on the [AKC website](https://www.akc.org/) to see how they look! -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "13b5f3e7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "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>breed</th>\n",
       "      <th>group</th>\n",
       "      <th>datadog</th>\n",
       "      <th>popularity_all</th>\n",
       "      <th>...</th>\n",
       "      <th>megarank</th>\n",
       "      <th>size</th>\n",
       "      <th>weight</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Border Collie</td>\n",
       "      <td>herding</td>\n",
       "      <td>3.64</td>\n",
       "      <td>45</td>\n",
       "      <td>...</td>\n",
       "      <td>29.0</td>\n",
       "      <td>medium</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Border Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>3.61</td>\n",
       "      <td>80</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>small</td>\n",
       "      <td>13.5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Brittany</td>\n",
       "      <td>sporting</td>\n",
       "      <td>3.54</td>\n",
       "      <td>30</td>\n",
       "      <td>...</td>\n",
       "      <td>11.0</td>\n",
       "      <td>medium</td>\n",
       "      <td>35.0</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>Wire Fox Terrier</td>\n",
       "      <td>terrier</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>small</td>\n",
       "      <td>17.5</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>170</th>\n",
       "      <td>Wirehaired Pointing Griffon</td>\n",
       "      <td>sporting</td>\n",
       "      <td>NaN</td>\n",
       "      <td>92</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>medium</td>\n",
       "      <td>NaN</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>Xoloitzcuintli</td>\n",
       "      <td>non-sporting</td>\n",
       "      <td>NaN</td>\n",
       "      <td>155</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>medium</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>172 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           breed         group  datadog  popularity_all  ...  \\\n",
       "0                  Border Collie       herding     3.64              45  ...   \n",
       "1                 Border Terrier       terrier     3.61              80  ...   \n",
       "2                       Brittany      sporting     3.54              30  ...   \n",
       "..                           ...           ...      ...             ...  ...   \n",
       "169             Wire Fox Terrier       terrier      NaN             100  ...   \n",
       "170  Wirehaired Pointing Griffon      sporting      NaN              92  ...   \n",
       "171               Xoloitzcuintli  non-sporting      NaN             155  ...   \n",
       "\n",
       "     megarank    size  weight  height  \n",
       "0        29.0  medium     NaN    20.0  \n",
       "1         1.0   small    13.5     NaN  \n",
       "2        11.0  medium    35.0    19.0  \n",
       "..        ...     ...     ...     ...  \n",
       "169       NaN   small    17.5    15.0  \n",
       "170       NaN  medium     NaN    22.0  \n",
       "171       NaN  medium     NaN    16.5  \n",
       "\n",
       "[172 rows x 18 columns]"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_dogs = pd.read_csv(Path('data') / 'all_dogs.csv')\n",
    "all_dogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "a1296237",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Your code goes here."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78158504",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## Summary, next time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b89b6ab7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Summary\n",
    "\n",
    "- `pandas` is **the** library for tabular data manipulation in Python.\n",
    "- There are three key data structures in `pandas`: DataFrame, Series, and Index.\n",
    "- Refer to the lecture notebook and the [`pandas` documentation](https://pandas.pydata.org/docs/user_guide/index.html#user-guide) for tips.\n",
    "- `pandas` relies heavily on `numpy`. An understanding of how data types work in both will allow you to write more efficient and bug-free code.\n",
    "- Series and DataFrames share many methods (refer to the [`pandas` documentation](https://pandas.pydata.org/docs/user_guide/index.html#user-guide) for more details).\n",
    "- Most `pandas` methods return copies of Series/DataFrames. Be careful when using techniques that modify values in-place.\n",
    "- Next time: `groupby` and data granularity."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa8f9568-b117-485b-b3e8-25bf10e59d90",
   "metadata": {
    "temporary": true
   },
   "source": [
    "### DSC 80 Calendar\n",
    "\n",
    "This webpage contains a calendar for DSC 80 lectures and office hours. It lists the lecture location as SOLIS 104 and explains where office hours will be held during the week and on Saturdays."
   ]
  }
 ],
 "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.21"
  },
  "livereveal": {
   "scroll": true
  },
  "rise": {
   "transition": "none"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
