{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "245f0c86",
   "metadata": {},
   "source": [
    "\n",
    "# Chapter 2\n",
    "\n",
    "# Lab: Introduction to Python\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ab29948",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed622870",
   "metadata": {},
   "source": [
    "To run the labs in this book, you will need two things:\n",
    "\n",
    "* An installation of `Python3`, which is the specific version of `Python`  used in the labs. \n",
    "* Access to  `Jupyter`, a very popular `Python` interface that runs code through a file called a *notebook*. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844d37fc",
   "metadata": {},
   "source": [
    "You can download and install  `Python3`   by following the instructions available at [anaconda.com](http://anaconda.com). "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "462ff1fe",
   "metadata": {},
   "source": [
    " There are a number of ways to get access to `Jupyter`. Here are just a few:\n",
    " \n",
    " * Using Google's `Colaboratory` service: [colab.research.google.com/](https://colab.research.google.com/). \n",
    " * Using `JupyterHub`, available at [jupyter.org/hub](https://jupyter.org/hub). \n",
    " * Using your own `jupyter` installation. Installation instructions are available at [jupyter.org/install](https://jupyter.org/install). \n",
    " \n",
    "Please see the `Python` resources page on the book website [statlearning.com](https://www.statlearning.com) for up-to-date information about getting `Python` and `Jupyter` working on your computer. \n",
    "\n",
    "You will need to install the `ISLP` package, which provides access to the datasets and custom-built functions that we provide.\n",
    "Inside a macOS or Linux terminal type `pip install ISLP`; this also installs most other packages needed in the labs. The `Python` resources page has a link to the `ISLP` documentation website.\n",
    "\n",
    "To run this lab, download the file `Ch2-statlearn-lab.ipynb` from the `Python` resources page. \n",
    "Now run the following code at the command line: `jupyter lab Ch2-statlearn-lab.ipynb`.\n",
    "\n",
    "If you're using Windows, you can use the `start menu` to access `anaconda`, and follow the links. For example, to install `ISLP` and run this lab, you can run the same code above in an `anaconda` shell.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b46f9182",
   "metadata": {},
   "source": [
    "## Basic Commands\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54060fd9",
   "metadata": {},
   "source": [
    "In this lab, we will introduce some simple `Python` commands. \n",
    " For more resources about `Python` in general, readers may want to consult the tutorial at [docs.python.org/3/tutorial/](https://docs.python.org/3/tutorial/). \n",
    "\n",
    "\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3dbd0e9",
   "metadata": {},
   "source": [
    "Like most programming languages, `Python` uses *functions*\n",
    "to perform operations.   To run a\n",
    "function called `fun`, we type\n",
    "`fun(input1,input2)`, where the inputs (or *arguments*)\n",
    "`input1` and `input2` tell\n",
    "`Python` how to run the function.  A function can have any number of\n",
    "inputs. For example, the\n",
    "`print()`  function outputs a text representation of all of its arguments to the console."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e8aa21f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.654555Z",
     "iopub.status.busy": "2023-08-21T03:36:04.654242Z",
     "iopub.status.idle": "2023-08-21T03:36:04.664431Z",
     "shell.execute_reply": "2023-08-21T03:36:04.663990Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fit a model with 11 variables\n"
     ]
    }
   ],
   "source": [
    "print('fit a model with', 11, 'variables')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d935f8",
   "metadata": {},
   "source": [
    " The following command will provide information about the `print()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d62ec119",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.666908Z",
     "iopub.status.busy": "2023-08-21T03:36:04.666727Z",
     "iopub.status.idle": "2023-08-21T03:36:04.693658Z",
     "shell.execute_reply": "2023-08-21T03:36:04.693389Z"
    }
   },
   "outputs": [],
   "source": [
    "print?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b3e2a3",
   "metadata": {},
   "source": [
    "Adding two integers in `Python` is pretty intuitive."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c64e9f4d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.695168Z",
     "iopub.status.busy": "2023-08-21T03:36:04.695071Z",
     "iopub.status.idle": "2023-08-21T03:36:04.697996Z",
     "shell.execute_reply": "2023-08-21T03:36:04.697736Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 + 5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd754cba",
   "metadata": {},
   "source": [
    "In `Python`, textual data is handled using\n",
    "*strings*. For instance, `\"hello\"` and\n",
    "`'hello'`\n",
    "are strings. \n",
    "We can concatenate them using the addition `+` symbol."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9abccc1f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.699502Z",
     "iopub.status.busy": "2023-08-21T03:36:04.699420Z",
     "iopub.status.idle": "2023-08-21T03:36:04.701419Z",
     "shell.execute_reply": "2023-08-21T03:36:04.701183Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello\" + \" \" + \"world\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c28db903",
   "metadata": {},
   "source": [
    " A string is actually a type of *sequence*: this is a generic term for an ordered list. \n",
    " The three most important types of sequences are lists, tuples, and strings.  \n",
    "We introduce lists now. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fdcc5a1",
   "metadata": {},
   "source": [
    "The following command instructs `Python` to join together\n",
    "the numbers 3, 4, and 5, and to save them as a\n",
    "*list* named `x`. When we\n",
    "type `x`, it gives us back the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "802ca33c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.702877Z",
     "iopub.status.busy": "2023-08-21T03:36:04.702786Z",
     "iopub.status.idle": "2023-08-21T03:36:04.704849Z",
     "shell.execute_reply": "2023-08-21T03:36:04.704596Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 4, 5]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [3, 4, 5]\n",
    "x\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5492ecd1",
   "metadata": {},
   "source": [
    "Note that we used the brackets\n",
    "`[]` to construct this list. \n",
    "\n",
    "We will often want to add two sets of numbers together. It is reasonable to try the following code,\n",
    "though it will not produce the desired results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a8c72744",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.706312Z",
     "iopub.status.busy": "2023-08-21T03:36:04.706207Z",
     "iopub.status.idle": "2023-08-21T03:36:04.708359Z",
     "shell.execute_reply": "2023-08-21T03:36:04.708116Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 4, 5, 4, 9, 7]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = [4, 9, 7]\n",
    "x + y\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f42ea1d",
   "metadata": {},
   "source": [
    "The result may appear slightly counterintuitive: why did `Python` not add the entries of the lists\n",
    "element-by-element? \n",
    " In `Python`, lists hold *arbitrary* objects, and  are added using  *concatenation*. \n",
    " In fact, concatenation is the behavior that we saw earlier when we entered `\"hello\" + \" \" + \"world\"`. \n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69015df5",
   "metadata": {},
   "source": [
    "This example reflects the fact that \n",
    " `Python` is a general-purpose programming language. Much of `Python`'s  data-specific\n",
    "functionality comes from other packages, notably `numpy`\n",
    "and `pandas`. \n",
    "In the next section, we will introduce the  `numpy` package. \n",
    "See [docs.scipy.org/doc/numpy/user/quickstart.html](https://docs.scipy.org/doc/numpy/user/quickstart.html) for more information about `numpy`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16bfc4a2",
   "metadata": {},
   "source": [
    "## Introduction to Numerical Python\n",
    "\n",
    "As mentioned earlier, this book makes use of functionality   that is contained in the `numpy` \n",
    " *library*, or *package*. A package is a collection of modules that are not necessarily included in \n",
    " the base `Python` distribution. The name `numpy` is an abbreviation for *numerical Python*. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5bed3f0",
   "metadata": {},
   "source": [
    "  To access `numpy`, we must first `import` it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f1c7d1db",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.709873Z",
     "iopub.status.busy": "2023-08-21T03:36:04.709781Z",
     "iopub.status.idle": "2023-08-21T03:36:04.767425Z",
     "shell.execute_reply": "2023-08-21T03:36:04.766663Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "import numpy as np "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c8614e7",
   "metadata": {},
   "source": [
    "In the previous line, we named the `numpy` *module* `np`; an abbreviation for easier referencing."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba1224a6",
   "metadata": {},
   "source": [
    "In `numpy`, an *array* is  a generic term for a multidimensional\n",
    "set of numbers.\n",
    "We use the `np.array()` function to define   `x` and `y`, which are one-dimensional arrays, i.e. vectors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e2ea2bfd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.772015Z",
     "iopub.status.busy": "2023-08-21T03:36:04.771631Z",
     "iopub.status.idle": "2023-08-21T03:36:04.777911Z",
     "shell.execute_reply": "2023-08-21T03:36:04.777302Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "x = np.array([3, 4, 5])\n",
    "y = np.array([4, 9, 7])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a977e05a",
   "metadata": {},
   "source": [
    "Note that if you forgot to run the `import numpy as np` command earlier, then\n",
    "you will encounter an error in calling the `np.array()` function in the previous line. \n",
    " The syntax `np.array()` indicates that the function being called\n",
    "is part of the `numpy` package, which we have abbreviated as `np`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "742431b6",
   "metadata": {},
   "source": [
    "Since `x` and `y` have been defined using `np.array()`, we get a sensible result when we add them together. Compare this to our results in the previous section,\n",
    " when we tried to add two lists without using `numpy`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "59fbf9fd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.781791Z",
     "iopub.status.busy": "2023-08-21T03:36:04.781430Z",
     "iopub.status.idle": "2023-08-21T03:36:04.788256Z",
     "shell.execute_reply": "2023-08-21T03:36:04.787586Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7, 13, 12])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ceccc2b",
   "metadata": {},
   "source": [
    "    \n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74be6d74",
   "metadata": {},
   "source": [
    "In `numpy`, matrices are typically represented as two-dimensional arrays, and vectors as one-dimensional arrays. {While it is also possible to create matrices using  `np.matrix()`, we will use `np.array()` throughout the labs in this book.}\n",
    "We can create a two-dimensional array as follows. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2279437e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.792995Z",
     "iopub.status.busy": "2023-08-21T03:36:04.792549Z",
     "iopub.status.idle": "2023-08-21T03:36:04.800387Z",
     "shell.execute_reply": "2023-08-21T03:36:04.799738Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1, 2], [3, 4]])\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f96f304d",
   "metadata": {},
   "source": [
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f764f7d1",
   "metadata": {},
   "source": [
    "The object `x` has several \n",
    "*attributes*, or associated objects. To access an attribute of `x`, we type `x.attribute`, where we replace `attribute`\n",
    "with the name of the attribute. \n",
    "For instance, we can access the `ndim` attribute of  `x` as follows. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "75bf1b1e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.804503Z",
     "iopub.status.busy": "2023-08-21T03:36:04.804231Z",
     "iopub.status.idle": "2023-08-21T03:36:04.810136Z",
     "shell.execute_reply": "2023-08-21T03:36:04.809353Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e3b83bf",
   "metadata": {},
   "source": [
    "The output indicates that `x` is a two-dimensional array.  \n",
    "Similarly, `x.dtype` is the *data type* attribute of the object `x`. This indicates that `x` is \n",
    "comprised of 64-bit integers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "58292240",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.813981Z",
     "iopub.status.busy": "2023-08-21T03:36:04.813417Z",
     "iopub.status.idle": "2023-08-21T03:36:04.819511Z",
     "shell.execute_reply": "2023-08-21T03:36:04.818936Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf9cf94b",
   "metadata": {},
   "source": [
    "Why is `x` comprised of integers? This is because we created `x` by passing in exclusively integers to the `np.array()` function.\n",
    "  If\n",
    "we had passed in any decimals, then we would have obtained an array of\n",
    "*floating point numbers* (i.e. real-valued numbers). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fc5fff57",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.823519Z",
     "iopub.status.busy": "2023-08-21T03:36:04.823222Z",
     "iopub.status.idle": "2023-08-21T03:36:04.829861Z",
     "shell.execute_reply": "2023-08-21T03:36:04.829223Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1, 2], [3.0, 4]]).dtype\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a79641",
   "metadata": {},
   "source": [
    "Typing `fun?` will cause `Python` to display \n",
    "documentation associated with the function `fun`, if it exists.\n",
    "We can try this for `np.array()`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "762562a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.833747Z",
     "iopub.status.busy": "2023-08-21T03:36:04.833468Z",
     "iopub.status.idle": "2023-08-21T03:36:04.838421Z",
     "shell.execute_reply": "2023-08-21T03:36:04.837747Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "np.array?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4d82167",
   "metadata": {},
   "source": [
    "This documentation indicates that we could create a floating point array by passing a `dtype` argument into `np.array()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "66d2b82a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.842127Z",
     "iopub.status.busy": "2023-08-21T03:36:04.841891Z",
     "iopub.status.idle": "2023-08-21T03:36:04.848885Z",
     "shell.execute_reply": "2023-08-21T03:36:04.848125Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1, 2], [3, 4]], float).dtype\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e3ba5be",
   "metadata": {},
   "source": [
    "The array `x` is two-dimensional. We can find out the number of rows and columns by looking\n",
    "at its `shape` attribute."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "89881402",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.853039Z",
     "iopub.status.busy": "2023-08-21T03:36:04.852643Z",
     "iopub.status.idle": "2023-08-21T03:36:04.859072Z",
     "shell.execute_reply": "2023-08-21T03:36:04.858149Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2967b644",
   "metadata": {},
   "source": [
    "A *method* is a function that is associated with an\n",
    "object. \n",
    "For instance, given an array `x`, the expression\n",
    "`x.sum()` sums all of its elements, using the `sum()`\n",
    "method for arrays. \n",
    "The call `x.sum()` automatically provides `x` as the\n",
    "first argument to its `sum()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0572d3f6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.863011Z",
     "iopub.status.busy": "2023-08-21T03:36:04.862610Z",
     "iopub.status.idle": "2023-08-21T03:36:04.869722Z",
     "shell.execute_reply": "2023-08-21T03:36:04.869078Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 4])\n",
    "x.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3f49995",
   "metadata": {},
   "source": [
    "We could also sum the elements of `x` by passing in `x` as an argument to the `np.sum()` function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "33b10a6f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.874228Z",
     "iopub.status.busy": "2023-08-21T03:36:04.873632Z",
     "iopub.status.idle": "2023-08-21T03:36:04.879222Z",
     "shell.execute_reply": "2023-08-21T03:36:04.878606Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 4])\n",
    "np.sum(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f3dd2c3",
   "metadata": {},
   "source": [
    " As another example, the\n",
    "`reshape()` method returns a new array with the same elements as\n",
    "`x`, but a different shape.\n",
    " We do this by passing in a `tuple` in our call to\n",
    " `reshape()`, in this case `(2, 3)`.  This tuple specifies that we would like to create a two-dimensional array with \n",
    "$2$ rows and $3$ columns. {Like lists, tuples represent a sequence of objects. Why do we need more than one way to create a sequence? There are a few differences between tuples and lists, but perhaps the most important is that elements of a tuple cannot be modified, whereas elements of a list can be.}\n",
    " \n",
    "In what follows, the\n",
    "`\\n` character creates a *new line*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a32716db",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.882833Z",
     "iopub.status.busy": "2023-08-21T03:36:04.882354Z",
     "iopub.status.idle": "2023-08-21T03:36:04.887384Z",
     "shell.execute_reply": "2023-08-21T03:36:04.886127Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "beginning x:\n",
      " [1 2 3 4 5 6]\n",
      "reshaped x:\n",
      " [[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 4, 5, 6])\n",
    "print('beginning x:\\n', x)\n",
    "x_reshape = x.reshape((2, 3))\n",
    "print('reshaped x:\\n', x_reshape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2483179e",
   "metadata": {},
   "source": [
    "The previous output reveals that `numpy` arrays are specified as a sequence\n",
    "of *rows*. This is  called *row-major ordering*, as opposed to *column-major ordering*. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e256575f",
   "metadata": {},
   "source": [
    "`Python` (and hence `numpy`) uses 0-based\n",
    "indexing. This means that to access the top left element of `x_reshape`, \n",
    "we type in `x_reshape[0,0]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3db6e1cf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.890861Z",
     "iopub.status.busy": "2023-08-21T03:36:04.890532Z",
     "iopub.status.idle": "2023-08-21T03:36:04.894815Z",
     "shell.execute_reply": "2023-08-21T03:36:04.894149Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_reshape[0, 0] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e10119e",
   "metadata": {},
   "source": [
    "Similarly, `x_reshape[1,2]` yields the element in the second row and the third column \n",
    "of `x_reshape`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e15c753f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.898049Z",
     "iopub.status.busy": "2023-08-21T03:36:04.897796Z",
     "iopub.status.idle": "2023-08-21T03:36:04.902137Z",
     "shell.execute_reply": "2023-08-21T03:36:04.901538Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_reshape[1, 2] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9c55622",
   "metadata": {},
   "source": [
    "Similarly, `x[2]` yields the\n",
    "third entry of `x`. \n",
    "\n",
    "Now, let's modify the top left element of `x_reshape`.  To our surprise, we discover that the first element of `x` has been modified as well!\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "91c6e7d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.905358Z",
     "iopub.status.busy": "2023-08-21T03:36:04.905148Z",
     "iopub.status.idle": "2023-08-21T03:36:04.910143Z",
     "shell.execute_reply": "2023-08-21T03:36:04.909500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x before we modify x_reshape:\n",
      " [1 2 3 4 5 6]\n",
      "x_reshape before we modify x_reshape:\n",
      " [[1 2 3]\n",
      " [4 5 6]]\n",
      "x_reshape after we modify its top left element:\n",
      " [[5 2 3]\n",
      " [4 5 6]]\n",
      "x after we modify top left element of x_reshape:\n",
      " [5 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "print('x before we modify x_reshape:\\n', x)\n",
    "print('x_reshape before we modify x_reshape:\\n', x_reshape)\n",
    "x_reshape[0, 0] = 5\n",
    "print('x_reshape after we modify its top left element:\\n', x_reshape)\n",
    "print('x after we modify top left element of x_reshape:\\n', x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a840507",
   "metadata": {},
   "source": [
    "Modifying `x_reshape` also modified `x` because the two objects occupy the same space in memory.\n",
    " \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec551f3e",
   "metadata": {},
   "source": [
    "We just saw that we can modify an element of an array. Can we also modify a tuple? It turns out that we cannot --- and trying to do so introduces\n",
    "an *exception*, or error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "59d95dce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:04.913411Z",
     "iopub.status.busy": "2023-08-21T03:36:04.912985Z",
     "iopub.status.idle": "2023-08-21T03:36:05.037046Z",
     "shell.execute_reply": "2023-08-21T03:36:05.036755Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[23], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m my_tuple \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mmy_tuple\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "my_tuple = (3, 4, 5)\n",
    "my_tuple[0] = 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d594f1af",
   "metadata": {},
   "source": [
    "We now briefly mention some attributes of arrays that will come in handy. An array's `shape` attribute contains its dimension; this is always a tuple.\n",
    "The  `ndim` attribute yields the number of dimensions, and `T` provides its transpose. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a6fde9af",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.038645Z",
     "iopub.status.busy": "2023-08-21T03:36:05.038536Z",
     "iopub.status.idle": "2023-08-21T03:36:05.040823Z",
     "shell.execute_reply": "2023-08-21T03:36:05.040589Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2, 3),\n",
       " 2,\n",
       " array([[5, 4],\n",
       "        [2, 5],\n",
       "        [3, 6]]))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_reshape.shape, x_reshape.ndim, x_reshape.T\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76d20b98",
   "metadata": {},
   "source": [
    "Notice that the three individual outputs `(2,3)`, `2`, and `array([[5, 4],[2, 5], [3,6]])` are themselves output as a tuple. \n",
    " \n",
    "We will often want to apply functions to arrays. \n",
    "For instance, we can compute the\n",
    "square root of the entries using the `np.sqrt()` function: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fadb6b45",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.042203Z",
     "iopub.status.busy": "2023-08-21T03:36:05.042112Z",
     "iopub.status.idle": "2023-08-21T03:36:05.044185Z",
     "shell.execute_reply": "2023-08-21T03:36:05.043940Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.23606798, 1.41421356, 1.73205081, 2.        , 2.23606798,\n",
       "       2.44948974])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22fab2ce",
   "metadata": {},
   "source": [
    "We can also square the elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fda3134b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.045709Z",
     "iopub.status.busy": "2023-08-21T03:36:05.045601Z",
     "iopub.status.idle": "2023-08-21T03:36:05.047665Z",
     "shell.execute_reply": "2023-08-21T03:36:05.047412Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([25,  4,  9, 16, 25, 36])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x**2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1278f26b",
   "metadata": {},
   "source": [
    "We can compute the square roots using the same notation, raising to the power of $1/2$ instead of 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "52eb335b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.049290Z",
     "iopub.status.busy": "2023-08-21T03:36:05.049191Z",
     "iopub.status.idle": "2023-08-21T03:36:05.051249Z",
     "shell.execute_reply": "2023-08-21T03:36:05.051004Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.23606798, 1.41421356, 1.73205081, 2.        , 2.23606798,\n",
       "       2.44948974])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x**0.5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "299a5a85",
   "metadata": {},
   "source": [
    "Throughout this book, we will often want to generate random data. \n",
    "The `np.random.normal()`  function generates a vector of random\n",
    "normal variables. We can learn more about this function by looking at the help page, via a call to `np.random.normal?`.\n",
    "The first line of the help page  reads `normal(loc=0.0, scale=1.0, size=None)`. \n",
    " This  *signature* line tells us that the function's arguments are  `loc`, `scale`, and `size`. These are *keyword* arguments, which means that when they are passed into\n",
    " the function, they can be referred to by name (in any order). {`Python` also uses *positional* arguments. Positional arguments do not need to use a keyword. To see an example, type in `np.sum?`. We see that `a` is a positional argument, i.e. this function assumes that the first unnamed argument that it receives is the array to be summed. By contrast, `axis` and `dtype` are keyword arguments: the position in which these arguments are entered into `np.sum()` does not matter.}\n",
    " By default, this function will generate random normal variable(s) with mean (`loc`) $0$ and standard deviation (`scale`) $1$; furthermore, \n",
    " a single random variable will be generated unless the argument to `size` is changed. \n",
    "\n",
    "We now generate 50 independent random variables from a $N(0,1)$ distribution. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ac5e9d29",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.052622Z",
     "iopub.status.busy": "2023-08-21T03:36:05.052529Z",
     "iopub.status.idle": "2023-08-21T03:36:05.054818Z",
     "shell.execute_reply": "2023-08-21T03:36:05.054569Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.97992307,  0.29561095,  0.60481929, -0.20482365, -1.01517527,\n",
       "        2.76594674, -0.65748447,  1.27396181, -1.01573869,  1.37373688,\n",
       "        0.93524901, -2.41971622, -0.58080017,  0.13777341,  1.35936356,\n",
       "        0.61747629,  0.82550921,  0.7943598 ,  1.0905082 ,  1.80820045,\n",
       "       -1.31320321,  2.0651966 , -0.78632522, -2.01068042, -1.36562571,\n",
       "        1.43466837, -0.16848093, -1.57211786,  0.49888498, -1.86050966,\n",
       "        1.08743609,  0.53059599,  0.02172848, -0.41808062,  1.88343414,\n",
       "        0.09112977,  1.23489568,  0.53207714, -1.62670383,  1.29859234,\n",
       "        0.30311344,  1.01143392, -1.64596169,  1.58876421, -1.85189683,\n",
       "       -0.53929878, -2.19895144, -0.36691225,  1.03621761,  0.25278481])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.normal(size=50)\n",
    "x\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d77cf45a",
   "metadata": {},
   "source": [
    "We create an array `y` by adding an independent $N(50,1)$ random variable to each element of `x`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "55fa905e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.056219Z",
     "iopub.status.busy": "2023-08-21T03:36:05.056124Z",
     "iopub.status.idle": "2023-08-21T03:36:05.057790Z",
     "shell.execute_reply": "2023-08-21T03:36:05.057543Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "y = x + np.random.normal(loc=50, scale=1, size=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eacfecc9",
   "metadata": {},
   "source": [
    "The `np.corrcoef()` function computes the correlation matrix between `x` and `y`. The off-diagonal elements give the \n",
    "correlation between `x` and `y`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fde0dc19",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.059163Z",
     "iopub.status.busy": "2023-08-21T03:36:05.059091Z",
     "iopub.status.idle": "2023-08-21T03:36:05.061351Z",
     "shell.execute_reply": "2023-08-21T03:36:05.061105Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.66045794],\n",
       "       [0.66045794, 1.        ]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a594218",
   "metadata": {},
   "source": [
    "If you're following along in your own `Jupyter` notebook, then you probably noticed that you got a different set of results when you ran the past few \n",
    "commands. In particular, \n",
    " each\n",
    "time we call `np.random.normal()`, we will get a different answer, as shown in the following example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5099cf54",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.062721Z",
     "iopub.status.busy": "2023-08-21T03:36:05.062630Z",
     "iopub.status.idle": "2023-08-21T03:36:05.064539Z",
     "shell.execute_reply": "2023-08-21T03:36:05.064300Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-9.63514647 -0.12742473]\n",
      "[0.85490033 0.05488893]\n"
     ]
    }
   ],
   "source": [
    "print(np.random.normal(scale=5, size=2))\n",
    "print(np.random.normal(scale=5, size=2)) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e209118",
   "metadata": {},
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed7697a4",
   "metadata": {},
   "source": [
    "In order to ensure that our code provides exactly the same results\n",
    "each time it is run, we can set a *random seed* \n",
    "using the \n",
    "`np.random.default_rng()` function.\n",
    "This function takes an arbitrary, user-specified integer argument. If we set a random seed before \n",
    "generating random data, then re-running our code will yield the same results. The\n",
    "object `rng` has essentially all the random number generating methods found in `np.random`. Hence, to\n",
    "generate normal data we use `rng.normal()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9d8074e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.065999Z",
     "iopub.status.busy": "2023-08-21T03:36:05.065912Z",
     "iopub.status.idle": "2023-08-21T03:36:05.068039Z",
     "shell.execute_reply": "2023-08-21T03:36:05.067820Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4.09482632 -1.07485605]\n",
      "[ 4.09482632 -1.07485605]\n"
     ]
    }
   ],
   "source": [
    "rng = np.random.default_rng(1303)\n",
    "print(rng.normal(scale=5, size=2))\n",
    "rng2 = np.random.default_rng(1303)\n",
    "print(rng2.normal(scale=5, size=2)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93f826ef",
   "metadata": {},
   "source": [
    "Throughout the labs in this book, we use `np.random.default_rng()`  whenever we\n",
    "perform calculations involving random quantities within `numpy`.  In principle, this\n",
    "should enable the reader to exactly reproduce the stated results. However, as new versions of `numpy` become available, it is possible\n",
    "that some small discrepancies may occur between the output\n",
    "in the labs and the output\n",
    "from `numpy`.\n",
    "\n",
    "The `np.mean()`,  `np.var()`, and `np.std()`  functions can be used\n",
    "to compute the mean, variance, and standard deviation of arrays.  These functions are also\n",
    "available as methods on the arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e98472df",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.069438Z",
     "iopub.status.busy": "2023-08-21T03:36:05.069347Z",
     "iopub.status.idle": "2023-08-21T03:36:05.071564Z",
     "shell.execute_reply": "2023-08-21T03:36:05.071324Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.1126795190952861, -0.1126795190952861)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = np.random.default_rng(3)\n",
    "y = rng.standard_normal(10)\n",
    "np.mean(y), y.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2870d61f",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8c2784fd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.072967Z",
     "iopub.status.busy": "2023-08-21T03:36:05.072873Z",
     "iopub.status.idle": "2023-08-21T03:36:05.075053Z",
     "shell.execute_reply": "2023-08-21T03:36:05.074833Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.7243406406465125, 2.7243406406465125, 2.7243406406465125)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(y), y.var(), np.mean((y - y.mean())**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86261a69",
   "metadata": {},
   "source": [
    "Notice that by default `np.var()` divides by the sample size $n$ rather\n",
    "than $n-1$; see the `ddof` argument in `np.var?`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7e7205f2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.076473Z",
     "iopub.status.busy": "2023-08-21T03:36:05.076373Z",
     "iopub.status.idle": "2023-08-21T03:36:05.078474Z",
     "shell.execute_reply": "2023-08-21T03:36:05.078224Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.6505576756498128, 1.6505576756498128)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.var(y)), np.std(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4faf901",
   "metadata": {},
   "source": [
    "The `np.mean()`,  `np.var()`, and `np.std()` functions can also be applied to the rows and columns of a matrix. \n",
    "To see this, we construct a $10 \\times 3$ matrix of $N(0,1)$ random variables, and consider computing its row sums. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "fce06849",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.079856Z",
     "iopub.status.busy": "2023-08-21T03:36:05.079755Z",
     "iopub.status.idle": "2023-08-21T03:36:05.081961Z",
     "shell.execute_reply": "2023-08-21T03:36:05.081709Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.22578661, -0.35263079, -0.28128742],\n",
       "       [-0.66804635, -1.05515055, -0.39080098],\n",
       "       [ 0.48194539, -0.23855361,  0.9577587 ],\n",
       "       [-0.19980213,  0.02425957,  1.54582085],\n",
       "       [ 0.54510552, -0.50522874, -0.18283897],\n",
       "       [ 0.54052513,  1.93508803, -0.26962033],\n",
       "       [-0.24355868,  1.0023136 , -0.88645994],\n",
       "       [-0.29172023,  0.88253897,  0.58035002],\n",
       "       [ 0.0915167 ,  0.67010435, -2.82816231],\n",
       "       [ 1.02130682, -0.95964476, -1.66861984]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = rng.standard_normal((10, 3))\n",
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cc355d2",
   "metadata": {},
   "source": [
    "Since arrays are row-major ordered, the first axis, i.e. `axis=0`, refers to its rows. We pass this argument into the `mean()` method for the object `X`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1403ff7a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.083361Z",
     "iopub.status.busy": "2023-08-21T03:36:05.083273Z",
     "iopub.status.idle": "2023-08-21T03:36:05.085366Z",
     "shell.execute_reply": "2023-08-21T03:36:05.085112Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.15030588,  0.14030961, -0.34238602])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.mean(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6785c0ec",
   "metadata": {},
   "source": [
    "The following yields the same result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7e9255ba",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.086730Z",
     "iopub.status.busy": "2023-08-21T03:36:05.086636Z",
     "iopub.status.idle": "2023-08-21T03:36:05.088698Z",
     "shell.execute_reply": "2023-08-21T03:36:05.088470Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.15030588,  0.14030961, -0.34238602])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.mean(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de246dc",
   "metadata": {},
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30b002fa",
   "metadata": {},
   "source": [
    "## Graphics\n",
    "In `Python`, common practice is to use  the library\n",
    "`matplotlib` for graphics.\n",
    "However, since `Python` was not written with data analysis in mind,\n",
    "  the notion of plotting is not intrinsic to the language. \n",
    "We will use the `subplots()` function\n",
    "from `matplotlib.pyplot` to create a figure and the\n",
    "axes onto which we plot our data.\n",
    "For many more examples of how to make plots in `Python`,\n",
    "readers are encouraged to visit [matplotlib.org/stable/gallery/](https://matplotlib.org/stable/gallery/index.html).\n",
    "\n",
    "In `matplotlib`, a plot consists of a *figure* and one or more *axes*. You can think of the figure as the blank canvas upon which \n",
    "one or more plots will be displayed: it is the entire plotting window. \n",
    "The *axes* contain important information about each plot, such as its $x$- and $y$-axis labels,\n",
    "title,  and more. (Note that in `matplotlib`, the word *axes* is not the plural of *axis*: a plot's *axes* contains much more information \n",
    "than just the $x$-axis and  the $y$-axis.)\n",
    "\n",
    "We begin by importing the `subplots()` function\n",
    "from `matplotlib`. We use this function\n",
    "throughout when creating figures.\n",
    "The function returns a tuple of length two: a figure\n",
    "object as well as the relevant axes object. We will typically\n",
    "pass `figsize` as a keyword argument.\n",
    "Having created our axes, we attempt our first plot using its  `plot()` method.\n",
    "To learn more about it, \n",
    "type `ax.plot?`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8236e5f7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.090105Z",
     "iopub.status.busy": "2023-08-21T03:36:05.090015Z",
     "iopub.status.idle": "2023-08-21T03:36:05.390499Z",
     "shell.execute_reply": "2023-08-21T03:36:05.390143Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 216116 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib.pyplot import subplots\n",
    "fig, ax = subplots(figsize=(8, 8))\n",
    "x = rng.standard_normal(100)\n",
    "y = rng.standard_normal(100)\n",
    "ax.plot(x, y);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbef67e6",
   "metadata": {},
   "source": [
    "We pause here to note that we have *unpacked* the tuple of length two returned by `subplots()` into the two distinct\n",
    "variables `fig` and `ax`. Unpacking\n",
    "is typically preferred to the following equivalent but slightly more verbose code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ddc9ed4f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.392242Z",
     "iopub.status.busy": "2023-08-21T03:36:05.392127Z",
     "iopub.status.idle": "2023-08-21T03:36:05.464507Z",
     "shell.execute_reply": "2023-08-21T03:36:05.464208Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12036 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "output = subplots(figsize=(8, 8))\n",
    "fig = output[0]\n",
    "ax = output[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "104d6b8f",
   "metadata": {},
   "source": [
    "We see that our earlier cell produced a line plot, which is the default. To create a scatterplot, we provide an additional argument to `ax.plot()`, indicating that circles should be displayed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c64ed600",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.466274Z",
     "iopub.status.busy": "2023-08-21T03:36:05.466157Z",
     "iopub.status.idle": "2023-08-21T03:36:05.540801Z",
     "shell.execute_reply": "2023-08-21T03:36:05.540543Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19912 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.plot(x, y, 'o');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "840be2a9",
   "metadata": {},
   "source": [
    "Different values\n",
    "of this additional argument can be used to produce different colored lines\n",
    "as well as different linestyles. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "971b98bd",
   "metadata": {},
   "source": [
    "As an alternative, we could use the  `ax.scatter()` function to create a scatterplot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bc6245e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.542497Z",
     "iopub.status.busy": "2023-08-21T03:36:05.542378Z",
     "iopub.status.idle": "2023-08-21T03:36:05.621061Z",
     "shell.execute_reply": "2023-08-21T03:36:05.620765Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19912 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.scatter(x, y, marker='o');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97f36df0",
   "metadata": {},
   "source": [
    "Notice that in the code blocks above, we have ended\n",
    "the last line with a semicolon. This prevents `ax.plot(x, y)` from printing\n",
    "text  to the notebook. However, it does not prevent a plot from being produced. \n",
    " If we omit the trailing semi-colon, then we obtain the following output:  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2454807b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.622738Z",
     "iopub.status.busy": "2023-08-21T03:36:05.622624Z",
     "iopub.status.idle": "2023-08-21T03:36:05.700853Z",
     "shell.execute_reply": "2023-08-21T03:36:05.700564Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x11ca12dd0>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19912 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.scatter(x, y, marker='o')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1230c0a6",
   "metadata": {},
   "source": [
    "In what follows, we will use\n",
    " trailing semicolons whenever the text that would be output is not\n",
    "germane to the discussion at hand.\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ccb9964",
   "metadata": {},
   "source": [
    "To label our plot, we  make use of the `set_xlabel()`,  `set_ylabel()`, and  `set_title()` methods\n",
    "of `ax`.\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1e18a793",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.702667Z",
     "iopub.status.busy": "2023-08-21T03:36:05.702545Z",
     "iopub.status.idle": "2023-08-21T03:36:05.797850Z",
     "shell.execute_reply": "2023-08-21T03:36:05.797545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29480 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.scatter(x, y, marker='o')\n",
    "ax.set_xlabel(\"this is the x-axis\")\n",
    "ax.set_ylabel(\"this is the y-axis\")\n",
    "ax.set_title(\"Plot of X vs Y\");"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d818ee",
   "metadata": {},
   "source": [
    " Having access to the figure object `fig` itself means that we can go in and change some aspects and then redisplay it. Here, we change\n",
    "  the size from `(8, 8)` to `(12, 3)`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "aec3f009",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.799658Z",
     "iopub.status.busy": "2023-08-21T03:36:05.799529Z",
     "iopub.status.idle": "2023-08-21T03:36:05.861021Z",
     "shell.execute_reply": "2023-08-21T03:36:05.860749Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25020 characters]",
      "text/plain": [
       "<Figure size 1200x300 with 1 Axes>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.set_size_inches(12,3)\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee531cc",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "011bf802",
   "metadata": {},
   "source": [
    "Occasionally we will want to create several plots within a figure. This can be\n",
    "achieved by passing additional arguments to `subplots()`. \n",
    "Below, we create a  $2 \\times 3$ grid of plots\n",
    "in a figure of size determined by the `figsize` argument. In such\n",
    "situations, there is often a relationship between the axes in the plots. For example,\n",
    "all plots may have a common $x$-axis. The `subplots()` function can automatically handle\n",
    "this situation when passed the keyword argument `sharex=True`.\n",
    "The `axes` object below is an array pointing to different plots in the figure. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2cbc7fd4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:05.862740Z",
     "iopub.status.busy": "2023-08-21T03:36:05.862621Z",
     "iopub.status.idle": "2023-08-21T03:36:06.143924Z",
     "shell.execute_reply": "2023-08-21T03:36:06.143646Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21324 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = subplots(nrows=2,\n",
    "                     ncols=3,\n",
    "                     figsize=(15, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ff2e6d",
   "metadata": {},
   "source": [
    "We now produce a scatter plot with `'o'` in the second column of the first row and\n",
    "a scatter plot with `'+'` in the third column of the second row."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "702f80d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:06.145683Z",
     "iopub.status.busy": "2023-08-21T03:36:06.145574Z",
     "iopub.status.idle": "2023-08-21T03:36:06.326301Z",
     "shell.execute_reply": "2023-08-21T03:36:06.326007Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40368 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 6 Axes>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "axes[0,1].plot(x, y, 'o')\n",
    "axes[1,2].scatter(x, y, marker='+')\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b265f8b",
   "metadata": {},
   "source": [
    "Type  `subplots?` to learn more about \n",
    "`subplots()`. \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bd7e707",
   "metadata": {},
   "source": [
    "To save the output of `fig`, we call its `savefig()`\n",
    "method. The argument `dpi` is the dots per inch, used\n",
    "to determine how large the figure will be in pixels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5493d229",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:06.327958Z",
     "iopub.status.busy": "2023-08-21T03:36:06.327847Z",
     "iopub.status.idle": "2023-08-21T03:36:07.432805Z",
     "shell.execute_reply": "2023-08-21T03:36:07.432429Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "fig.savefig(\"Figure.png\", dpi=400)\n",
    "fig.savefig(\"Figure.pdf\", dpi=200);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7152d0c7",
   "metadata": {},
   "source": [
    "We can continue to modify `fig` using step-by-step updates; for example, we can modify the range of the $x$-axis, re-save the figure, and even re-display it. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "bd07af12",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:07.434785Z",
     "iopub.status.busy": "2023-08-21T03:36:07.434614Z",
     "iopub.status.idle": "2023-08-21T03:36:07.663475Z",
     "shell.execute_reply": "2023-08-21T03:36:07.663165Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40260 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 6 Axes>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "axes[0,1].set_xlim([-1,1])\n",
    "fig.savefig(\"Figure_updated.jpg\")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5278857",
   "metadata": {},
   "source": [
    "We now create some more sophisticated plots. The \n",
    "`ax.contour()` method  produces a  *contour plot* \n",
    "in order to represent three-dimensional data, similar to a\n",
    "topographical map.  It takes three arguments:\n",
    "\n",
    "* A vector of `x` values (the first dimension),\n",
    "* A vector of `y` values (the second dimension), and\n",
    "* A matrix whose elements correspond to the `z` value (the third\n",
    "dimension) for each pair of `(x,y)` coordinates.\n",
    "\n",
    "To create `x` and `y`, we’ll use the command  `np.linspace(a, b, n)`, \n",
    "which returns a vector of `n` numbers starting at  `a` and  ending at `b`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "01019508",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:07.665255Z",
     "iopub.status.busy": "2023-08-21T03:36:07.665128Z",
     "iopub.status.idle": "2023-08-21T03:36:07.766569Z",
     "shell.execute_reply": "2023-08-21T03:36:07.766244Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 73608 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "x = np.linspace(-np.pi, np.pi, 50)\n",
    "y = x\n",
    "f = np.multiply.outer(np.cos(y), 1 / (1 + x**2))\n",
    "ax.contour(x, y, f);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef3c475",
   "metadata": {},
   "source": [
    "We can increase the resolution by adding more levels to the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7d08992f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:07.768244Z",
     "iopub.status.busy": "2023-08-21T03:36:07.768130Z",
     "iopub.status.idle": "2023-08-21T03:36:07.893027Z",
     "shell.execute_reply": "2023-08-21T03:36:07.892660Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 369236 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.contour(x, y, f, levels=45);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1d37a2",
   "metadata": {},
   "source": [
    "To fine-tune the output of the\n",
    "`ax.contour()`  function, take a\n",
    "look at the help file by typing `?plt.contour`.\n",
    " \n",
    "The `ax.imshow()`  method is similar to \n",
    "`ax.contour()`, except that it produces a color-coded plot\n",
    "whose colors depend on the `z` value. This is known as a\n",
    "*heatmap*, and is sometimes used to plot temperature in\n",
    "weather forecasts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "1f89d704",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:07.894802Z",
     "iopub.status.busy": "2023-08-21T03:36:07.894695Z",
     "iopub.status.idle": "2023-08-21T03:36:07.995202Z",
     "shell.execute_reply": "2023-08-21T03:36:07.994863Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19176 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.imshow(f);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2500a6ec",
   "metadata": {},
   "source": [
    "## Sequences and Slice Notation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07001b88",
   "metadata": {},
   "source": [
    "As seen above, the\n",
    "function `np.linspace()`  can be used to create a sequence\n",
    "of numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "cd971131",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:07.996961Z",
     "iopub.status.busy": "2023-08-21T03:36:07.996836Z",
     "iopub.status.idle": "2023-08-21T03:36:07.999447Z",
     "shell.execute_reply": "2023-08-21T03:36:07.999154Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq1 = np.linspace(0, 10, 11)\n",
    "seq1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "926f96fc",
   "metadata": {},
   "source": [
    "The function `np.arange()`\n",
    " returns a sequence of numbers spaced out by `step`. If `step` is not specified, then a default value of $1$ is used. Let's create a sequence\n",
    " that starts at $0$ and ends at $10$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "aa630d16",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.001028Z",
     "iopub.status.busy": "2023-08-21T03:36:08.000925Z",
     "iopub.status.idle": "2023-08-21T03:36:08.003185Z",
     "shell.execute_reply": "2023-08-21T03:36:08.002908Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq2 = np.arange(0, 10)\n",
    "seq2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6908bad7",
   "metadata": {},
   "source": [
    "Why isn't $10$ output above? This has to do with *slice* notation in `Python`. \n",
    "Slice notation  \n",
    "is used to index sequences such as lists, tuples and arrays.\n",
    "Suppose we want to retrieve the fourth through sixth (inclusive) entries\n",
    "of a string. We obtain a slice of the string using the indexing  notation  `[3:6]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "89955ee2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.004703Z",
     "iopub.status.busy": "2023-08-21T03:36:08.004594Z",
     "iopub.status.idle": "2023-08-21T03:36:08.006692Z",
     "shell.execute_reply": "2023-08-21T03:36:08.006429Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lo '"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello world\"[3:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17d73e4d",
   "metadata": {},
   "source": [
    "In the code block above, the notation `3:6` is shorthand for  `slice(3,6)` when used inside\n",
    "`[]`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "517f592d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.008158Z",
     "iopub.status.busy": "2023-08-21T03:36:08.008062Z",
     "iopub.status.idle": "2023-08-21T03:36:08.010083Z",
     "shell.execute_reply": "2023-08-21T03:36:08.009819Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lo '"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello world\"[slice(3,6)]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "680fe656",
   "metadata": {},
   "source": [
    "You might have expected  `slice(3,6)` to output the fourth through seventh characters in the text string (recalling that  `Python` begins its indexing at zero),  but instead it output  the fourth through sixth. \n",
    " This also explains why the earlier `np.arange(0, 10)` command output only the integers from $0$ to $9$. \n",
    "See the documentation `slice?` for useful options in creating slices. \n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "    \n",
    "\n",
    " \n",
    "\n",
    "    \n",
    "\n",
    " \n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "522a2761",
   "metadata": {},
   "source": [
    "## Indexing Data\n",
    "To begin, we  create a two-dimensional `numpy` array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "35927abd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.011518Z",
     "iopub.status.busy": "2023-08-21T03:36:08.011425Z",
     "iopub.status.idle": "2023-08-21T03:36:08.013558Z",
     "shell.execute_reply": "2023-08-21T03:36:08.013317Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array(np.arange(16)).reshape((4, 4))\n",
    "A\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27c88984",
   "metadata": {},
   "source": [
    "Typing `A[1,2]` retrieves the element corresponding to the second row and third\n",
    "column. (As usual, `Python` indexes from $0.$)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "78ee7f5b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.014962Z",
     "iopub.status.busy": "2023-08-21T03:36:08.014871Z",
     "iopub.status.idle": "2023-08-21T03:36:08.016938Z",
     "shell.execute_reply": "2023-08-21T03:36:08.016689Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[1,2]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd65ec1c",
   "metadata": {},
   "source": [
    "The first number after the open-bracket symbol `[`\n",
    " refers to the row, and the second number refers to the column. \n",
    "\n",
    "### Indexing Rows, Columns, and Submatrices\n",
    " To select multiple rows at a time, we can pass in a list\n",
    "  specifying our selection. For instance, `[1,3]` will retrieve the second and fourth rows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "16212696",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.018506Z",
     "iopub.status.busy": "2023-08-21T03:36:08.018407Z",
     "iopub.status.idle": "2023-08-21T03:36:08.020572Z",
     "shell.execute_reply": "2023-08-21T03:36:08.020301Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1,3]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b8b3ce3",
   "metadata": {},
   "source": [
    "To select the first and third columns, we pass in  `[0,2]` as the second argument in the square brackets.\n",
    "In this case we need to supply the first argument `:` \n",
    "which selects all rows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "d5f473d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.022048Z",
     "iopub.status.busy": "2023-08-21T03:36:08.021947Z",
     "iopub.status.idle": "2023-08-21T03:36:08.024301Z",
     "shell.execute_reply": "2023-08-21T03:36:08.024006Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [ 8, 10],\n",
       "       [12, 14]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,[0,2]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "471ed1b4",
   "metadata": {},
   "source": [
    "Now, suppose that we want to select the submatrix made up of the second and fourth \n",
    "rows as well as the first and third columns. This is where\n",
    "indexing gets slightly tricky. It is natural to try  to use lists to retrieve the rows and columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c89646d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.025864Z",
     "iopub.status.busy": "2023-08-21T03:36:08.025745Z",
     "iopub.status.idle": "2023-08-21T03:36:08.027990Z",
     "shell.execute_reply": "2023-08-21T03:36:08.027692Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 14])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1,3],[0,2]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cbf1ff9",
   "metadata": {},
   "source": [
    " Oops --- what happened? We got a one-dimensional array of length two identical to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "87f6b4f2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.029541Z",
     "iopub.status.busy": "2023-08-21T03:36:08.029439Z",
     "iopub.status.idle": "2023-08-21T03:36:08.031650Z",
     "shell.execute_reply": "2023-08-21T03:36:08.031378Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 14])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([A[1,0],A[3,2]])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a93dc96",
   "metadata": {},
   "source": [
    " Similarly,  the following code fails to extract the submatrix comprised of the second and fourth rows and the first, third, and fourth columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "5da5bda8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.033127Z",
     "iopub.status.busy": "2023-08-21T03:36:08.033016Z",
     "iopub.status.idle": "2023-08-21T03:36:08.056275Z",
     "shell.execute_reply": "2023-08-21T03:36:08.056014Z"
    }
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[63], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mA\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mIndexError\u001b[0m: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) "
     ]
    }
   ],
   "source": [
    "A[[1,3],[0,2,3]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4fd2f83",
   "metadata": {},
   "source": [
    "We can see what has gone wrong here. When supplied with two indexing lists, the `numpy` interpretation is that these provide pairs of $i,j$ indices for a series of entries. That is why the pair of lists must have the same length. However, that was not our intent, since we are looking for a submatrix.\n",
    "\n",
    "One easy way to do this is as follows. We first create a submatrix by subsetting the rows of `A`, and then on the fly we make a further submatrix by subsetting its columns.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ac48a95b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.057926Z",
     "iopub.status.busy": "2023-08-21T03:36:08.057815Z",
     "iopub.status.idle": "2023-08-21T03:36:08.060231Z",
     "shell.execute_reply": "2023-08-21T03:36:08.059980Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6],\n",
       "       [12, 14]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1,3]][:,[0,2]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e8388aa",
   "metadata": {},
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a09467cd",
   "metadata": {},
   "source": [
    "There are more efficient ways of achieving the same result.\n",
    "\n",
    "The *convenience function* `np.ix_()` allows us  to extract a submatrix\n",
    "using lists, by creating an intermediate *mesh* object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ee195cc4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.061798Z",
     "iopub.status.busy": "2023-08-21T03:36:08.061686Z",
     "iopub.status.idle": "2023-08-21T03:36:08.063979Z",
     "shell.execute_reply": "2023-08-21T03:36:08.063715Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6,  7],\n",
       "       [12, 14, 15]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = np.ix_([1,3],[0,2,3])\n",
    "A[idx]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7177cb9",
   "metadata": {},
   "source": [
    "Alternatively, we can subset matrices efficiently using slices.\n",
    "  \n",
    "The slice\n",
    "`1:4:2` captures the second and fourth items of a sequence, while the slice `0:3:2` captures\n",
    "the first and third items (the third element in a slice sequence is the step size)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "48917bb5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.067067Z",
     "iopub.status.busy": "2023-08-21T03:36:08.066955Z",
     "iopub.status.idle": "2023-08-21T03:36:08.069279Z",
     "shell.execute_reply": "2023-08-21T03:36:08.068966Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6],\n",
       "       [12, 14]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[1:4:2,0:3:2]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "697c5ab0",
   "metadata": {},
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c647dbf0",
   "metadata": {},
   "source": [
    "Why are we able to retrieve a submatrix directly using slices but not using lists?\n",
    "Its because they are different `Python` types, and\n",
    "are treated differently by `numpy`.\n",
    "Slices can be used to extract objects from arbitrary sequences, such as strings, lists, and tuples, while the use of lists for indexing is more limited.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "\n",
    " \n",
    "\n",
    "    \n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dce8961",
   "metadata": {},
   "source": [
    "### Boolean Indexing\n",
    "In `numpy`, a *Boolean* is a type  that equals either   `True` or  `False` (also represented as $1$ and $0$, respectively).\n",
    "The next line creates a vector of $0$'s, represented as Booleans, of length equal to the first dimension of `A`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "5d4caf22",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.070903Z",
     "iopub.status.busy": "2023-08-21T03:36:08.070799Z",
     "iopub.status.idle": "2023-08-21T03:36:08.072959Z",
     "shell.execute_reply": "2023-08-21T03:36:08.072701Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keep_rows = np.zeros(A.shape[0], bool)\n",
    "keep_rows"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d83fadb5",
   "metadata": {},
   "source": [
    "We now set two of the elements to `True`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "348820e3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.074422Z",
     "iopub.status.busy": "2023-08-21T03:36:08.074322Z",
     "iopub.status.idle": "2023-08-21T03:36:08.076398Z",
     "shell.execute_reply": "2023-08-21T03:36:08.076106Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True, False,  True])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keep_rows[[1,3]] = True\n",
    "keep_rows\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0fb487d",
   "metadata": {},
   "source": [
    "Note that the elements of `keep_rows`, when viewed as integers, are the same as the\n",
    "values of `np.array([0,1,0,1])`. Below, we use  `==` to verify their equality. When\n",
    "applied to two arrays, the `==`   operation is applied elementwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "4aafe45b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.077952Z",
     "iopub.status.busy": "2023-08-21T03:36:08.077853Z",
     "iopub.status.idle": "2023-08-21T03:36:08.080163Z",
     "shell.execute_reply": "2023-08-21T03:36:08.079901Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(keep_rows == np.array([0,1,0,1]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "603c0c53",
   "metadata": {},
   "source": [
    "(Here, the function `np.all()` has checked whether\n",
    "all entries of an array are `True`. A similar function, `np.any()`, can be used to check whether any entries of an array are `True`.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0a449d1",
   "metadata": {},
   "source": [
    "   However, even though `np.array([0,1,0,1])`  and `keep_rows` are equal according to `==`, they index different sets of rows!\n",
    "The former retrieves the first, second, first, and second rows of `A`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "1be6a588",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.081637Z",
     "iopub.status.busy": "2023-08-21T03:36:08.081537Z",
     "iopub.status.idle": "2023-08-21T03:36:08.083796Z",
     "shell.execute_reply": "2023-08-21T03:36:08.083505Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [4, 5, 6, 7],\n",
       "       [0, 1, 2, 3],\n",
       "       [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[np.array([0,1,0,1])]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e45bbebe",
   "metadata": {},
   "source": [
    " By contrast, `keep_rows` retrieves only the second and fourth rows  of `A` --- i.e. the rows for which the Boolean equals `TRUE`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "e83da57b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.085319Z",
     "iopub.status.busy": "2023-08-21T03:36:08.085216Z",
     "iopub.status.idle": "2023-08-21T03:36:08.087359Z",
     "shell.execute_reply": "2023-08-21T03:36:08.087095Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[keep_rows]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "374d34a7",
   "metadata": {},
   "source": [
    "This example shows that Booleans and integers are treated differently by `numpy`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25db74bf",
   "metadata": {},
   "source": [
    "We again make use of the `np.ix_()` function\n",
    " to create a mesh containing the second and fourth rows, and the first,  third, and fourth columns. This time, we apply the function to Booleans,\n",
    " rather than lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "09675294",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.088904Z",
     "iopub.status.busy": "2023-08-21T03:36:08.088804Z",
     "iopub.status.idle": "2023-08-21T03:36:08.091266Z",
     "shell.execute_reply": "2023-08-21T03:36:08.090985Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6,  7],\n",
       "       [12, 14, 15]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keep_cols = np.zeros(A.shape[1], bool)\n",
    "keep_cols[[0, 2, 3]] = True\n",
    "idx_bool = np.ix_(keep_rows, keep_cols)\n",
    "A[idx_bool]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0166c179",
   "metadata": {},
   "source": [
    "We can also mix a list with an array of Booleans in the arguments to `np.ix_()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "a85614e4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.092757Z",
     "iopub.status.busy": "2023-08-21T03:36:08.092664Z",
     "iopub.status.idle": "2023-08-21T03:36:08.094873Z",
     "shell.execute_reply": "2023-08-21T03:36:08.094581Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6,  7],\n",
       "       [12, 14, 15]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx_mixed = np.ix_([1,3], keep_cols)\n",
    "A[idx_mixed]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6a338f1",
   "metadata": {},
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3541e0c",
   "metadata": {},
   "source": [
    "For more details on indexing in `numpy`, readers are referred\n",
    "to the `numpy` tutorial mentioned earlier.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab75f168",
   "metadata": {},
   "source": [
    "## Loading Data\n",
    "\n",
    "Data sets often contain different types of data, and may have names associated with the rows or columns. \n",
    "For these reasons, they typically are best accommodated using a\n",
    " *data frame*. \n",
    " We can think of a data frame  as a sequence\n",
    "of arrays of identical length; these are the columns. Entries in the\n",
    "different arrays can be combined to form a row.\n",
    " The `pandas`\n",
    "library can be used to create and work with data frame objects."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca018d13",
   "metadata": {},
   "source": [
    "### Reading in a Data Set\n",
    "\n",
    "The first step of most analyses involves importing a data set into\n",
    "`Python`.  \n",
    " Before attempting to load\n",
    "a data set, we must make sure that `Python` knows where to find the file containing it. \n",
    "If the\n",
    "file is in the same location\n",
    "as this notebook file, then we are all set. \n",
    "Otherwise, \n",
    "the command\n",
    "`os.chdir()`  can be used to *change directory*. (You will need to call `import os` before calling `os.chdir()`.) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b76342df",
   "metadata": {},
   "source": [
    "We will begin by reading in `Auto.csv`, available on the book website. This is a comma-separated file, and can be read in using `pd.read_csv()`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "ff81e644",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.096546Z",
     "iopub.status.busy": "2023-08-21T03:36:08.096449Z",
     "iopub.status.idle": "2023-08-21T03:36:08.317557Z",
     "shell.execute_reply": "2023-08-21T03:36:08.317278Z"
    }
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307.0</td>\n",
       "      <td>130</td>\n",
       "      <td>3504</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350.0</td>\n",
       "      <td>165</td>\n",
       "      <td>3693</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318.0</td>\n",
       "      <td>150</td>\n",
       "      <td>3436</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150</td>\n",
       "      <td>3433</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>amc rebel sst</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140</td>\n",
       "      <td>3449</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>ford torino</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>387</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>140.0</td>\n",
       "      <td>86</td>\n",
       "      <td>2790</td>\n",
       "      <td>15.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford mustang gl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388</th>\n",
       "      <td>44.0</td>\n",
       "      <td>4</td>\n",
       "      <td>97.0</td>\n",
       "      <td>52</td>\n",
       "      <td>2130</td>\n",
       "      <td>24.6</td>\n",
       "      <td>82</td>\n",
       "      <td>2</td>\n",
       "      <td>vw pickup</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>389</th>\n",
       "      <td>32.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84</td>\n",
       "      <td>2295</td>\n",
       "      <td>11.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge rampage</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>390</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>120.0</td>\n",
       "      <td>79</td>\n",
       "      <td>2625</td>\n",
       "      <td>18.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford ranger</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>391</th>\n",
       "      <td>31.0</td>\n",
       "      <td>4</td>\n",
       "      <td>119.0</td>\n",
       "      <td>82</td>\n",
       "      <td>2720</td>\n",
       "      <td>19.4</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevy s-10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>392 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      mpg  cylinders  displacement  horsepower  weight  acceleration  year  \\\n",
       "0    18.0          8         307.0         130    3504          12.0    70   \n",
       "1    15.0          8         350.0         165    3693          11.5    70   \n",
       "2    18.0          8         318.0         150    3436          11.0    70   \n",
       "3    16.0          8         304.0         150    3433          12.0    70   \n",
       "4    17.0          8         302.0         140    3449          10.5    70   \n",
       "..    ...        ...           ...         ...     ...           ...   ...   \n",
       "387  27.0          4         140.0          86    2790          15.6    82   \n",
       "388  44.0          4          97.0          52    2130          24.6    82   \n",
       "389  32.0          4         135.0          84    2295          11.6    82   \n",
       "390  28.0          4         120.0          79    2625          18.6    82   \n",
       "391  31.0          4         119.0          82    2720          19.4    82   \n",
       "\n",
       "     origin                       name  \n",
       "0         1  chevrolet chevelle malibu  \n",
       "1         1          buick skylark 320  \n",
       "2         1         plymouth satellite  \n",
       "3         1              amc rebel sst  \n",
       "4         1                ford torino  \n",
       "..      ...                        ...  \n",
       "387       1            ford mustang gl  \n",
       "388       2                  vw pickup  \n",
       "389       1              dodge rampage  \n",
       "390       1                ford ranger  \n",
       "391       1                 chevy s-10  \n",
       "\n",
       "[392 rows x 9 columns]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "Auto = pd.read_csv('Auto.csv')\n",
    "Auto\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42d6a799",
   "metadata": {},
   "source": [
    "The book website also has a whitespace-delimited version of this data, called `Auto.data`. This can be read in as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "5b45aa7f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.319194Z",
     "iopub.status.busy": "2023-08-21T03:36:08.319082Z",
     "iopub.status.idle": "2023-08-21T03:36:08.322312Z",
     "shell.execute_reply": "2023-08-21T03:36:08.322063Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "Auto = pd.read_csv('Auto.data', delim_whitespace=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f942c457",
   "metadata": {},
   "source": [
    " Both `Auto.csv` and `Auto.data` are simply text\n",
    "files. Before loading data into `Python`, it is a good idea to view it using\n",
    "a text editor or other software, such as Microsoft Excel.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1aceff38",
   "metadata": {},
   "source": [
    "We now take a look at the column of `Auto` corresponding to the variable `horsepower`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "413f626a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.323776Z",
     "iopub.status.busy": "2023-08-21T03:36:08.323686Z",
     "iopub.status.idle": "2023-08-21T03:36:08.326230Z",
     "shell.execute_reply": "2023-08-21T03:36:08.325989Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      130.0\n",
       "1      165.0\n",
       "2      150.0\n",
       "3      150.0\n",
       "4      140.0\n",
       "       ...  \n",
       "392    86.00\n",
       "393    52.00\n",
       "394    84.00\n",
       "395    79.00\n",
       "396    82.00\n",
       "Name: horsepower, Length: 397, dtype: object"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto['horsepower']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd11e757",
   "metadata": {},
   "source": [
    "We see that the `dtype` of this column is `object`. \n",
    "It turns out that all values of the `horsepower` column were interpreted as strings when reading\n",
    "in the data. \n",
    "We can find out why by looking at the unique values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "57b86346",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.327630Z",
     "iopub.status.busy": "2023-08-21T03:36:08.327527Z",
     "iopub.status.idle": "2023-08-21T03:36:08.329871Z",
     "shell.execute_reply": "2023-08-21T03:36:08.329620Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['100.0', '102.0', '103.0', '105.0', '107.0', '108.0', '110.0',\n",
       "       '112.0', '113.0', '115.0', '116.0', '120.0', '122.0', '125.0',\n",
       "       '129.0', '130.0', '132.0', '133.0', '135.0', '137.0', '138.0',\n",
       "       '139.0', '140.0', '142.0', '145.0', '148.0', '149.0', '150.0',\n",
       "       '152.0', '153.0', '155.0', '158.0', '160.0', '165.0', '167.0',\n",
       "       '170.0', '175.0', '180.0', '190.0', '193.0', '198.0', '200.0',\n",
       "       '208.0', '210.0', '215.0', '220.0', '225.0', '230.0', '46.00',\n",
       "       '48.00', '49.00', '52.00', '53.00', '54.00', '58.00', '60.00',\n",
       "       '61.00', '62.00', '63.00', '64.00', '65.00', '66.00', '67.00',\n",
       "       '68.00', '69.00', '70.00', '71.00', '72.00', '74.00', '75.00',\n",
       "       '76.00', '77.00', '78.00', '79.00', '80.00', '81.00', '82.00',\n",
       "       '83.00', '84.00', '85.00', '86.00', '87.00', '88.00', '89.00',\n",
       "       '90.00', '91.00', '92.00', '93.00', '94.00', '95.00', '96.00',\n",
       "       '97.00', '98.00', '?'], dtype=object)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(Auto['horsepower'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0aee233",
   "metadata": {},
   "source": [
    "We see the culprit is the value `?`, which is being used to encode missing values.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7b032d4",
   "metadata": {},
   "source": [
    "To fix the problem, we must provide `pd.read_csv()` with an argument called `na_values`.\n",
    "Now,  each instance of  `?` in the file is replaced with the\n",
    "value `np.nan`, which means *not a number*:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "a9698b26",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.331328Z",
     "iopub.status.busy": "2023-08-21T03:36:08.331230Z",
     "iopub.status.idle": "2023-08-21T03:36:08.334947Z",
     "shell.execute_reply": "2023-08-21T03:36:08.334715Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40952.0"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto = pd.read_csv('Auto.data',\n",
    "                   na_values=['?'],\n",
    "                   delim_whitespace=True)\n",
    "Auto['horsepower'].sum()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13cb364e",
   "metadata": {},
   "source": [
    "The `Auto.shape`  attribute tells us that the data has 397\n",
    "observations, or rows, and nine variables, or columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "4877cb2c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.336400Z",
     "iopub.status.busy": "2023-08-21T03:36:08.336322Z",
     "iopub.status.idle": "2023-08-21T03:36:08.338340Z",
     "shell.execute_reply": "2023-08-21T03:36:08.338101Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(397, 9)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto.shape\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fdc6f47",
   "metadata": {},
   "source": [
    "There are\n",
    "various ways to deal with  missing data. \n",
    "In this case, since only five of the rows contain missing\n",
    "observations,  we choose to use the `Auto.dropna()` method to simply remove these rows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "2ba1d33d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.339714Z",
     "iopub.status.busy": "2023-08-21T03:36:08.339640Z",
     "iopub.status.idle": "2023-08-21T03:36:08.342116Z",
     "shell.execute_reply": "2023-08-21T03:36:08.341883Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(392, 9)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_new = Auto.dropna()\n",
    "Auto_new.shape\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac9748d9",
   "metadata": {},
   "source": [
    "### Basics of Selecting Rows and Columns\n",
    " \n",
    "We can use `Auto.columns`  to check the variable names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "3d03baab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.343548Z",
     "iopub.status.busy": "2023-08-21T03:36:08.343471Z",
     "iopub.status.idle": "2023-08-21T03:36:08.345627Z",
     "shell.execute_reply": "2023-08-21T03:36:08.345379Z"
    },
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',\n",
       "       'acceleration', 'year', 'origin', 'name'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto = Auto_new # overwrite the previous value\n",
    "Auto.columns\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d24d4d42",
   "metadata": {},
   "source": [
    "Accessing the rows and columns of a data frame is similar, but not identical, to accessing the rows and columns of an array. \n",
    "Recall that the first argument to the `[]` method\n",
    "is always applied to the rows of the array.  \n",
    "Similarly, \n",
    "passing in a slice to the `[]` method creates a data frame whose *rows* are determined by the slice:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "410b4dd7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.347031Z",
     "iopub.status.busy": "2023-08-21T03:36:08.346955Z",
     "iopub.status.idle": "2023-08-21T03:36:08.351303Z",
     "shell.execute_reply": "2023-08-21T03:36:08.351081Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>3504.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350.0</td>\n",
       "      <td>165.0</td>\n",
       "      <td>3693.0</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>3436.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement  horsepower  weight  acceleration  year  \\\n",
       "0  18.0          8         307.0       130.0  3504.0          12.0    70   \n",
       "1  15.0          8         350.0       165.0  3693.0          11.5    70   \n",
       "2  18.0          8         318.0       150.0  3436.0          11.0    70   \n",
       "\n",
       "   origin                       name  \n",
       "0       1  chevrolet chevelle malibu  \n",
       "1       1          buick skylark 320  \n",
       "2       1         plymouth satellite  "
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto[:3]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ea0be7b",
   "metadata": {},
   "source": [
    "Similarly, an array of Booleans can be used to subset the rows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "3540804d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.352671Z",
     "iopub.status.busy": "2023-08-21T03:36:08.352588Z",
     "iopub.status.idle": "2023-08-21T03:36:08.365290Z",
     "shell.execute_reply": "2023-08-21T03:36:08.365017Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>338</th>\n",
       "      <td>27.2</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2490.0</td>\n",
       "      <td>15.7</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth reliant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>339</th>\n",
       "      <td>26.6</td>\n",
       "      <td>4</td>\n",
       "      <td>151.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2635.0</td>\n",
       "      <td>16.4</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340</th>\n",
       "      <td>25.8</td>\n",
       "      <td>4</td>\n",
       "      <td>156.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2620.0</td>\n",
       "      <td>14.4</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge aries wagon (sw)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>23.5</td>\n",
       "      <td>6</td>\n",
       "      <td>173.0</td>\n",
       "      <td>110.0</td>\n",
       "      <td>2725.0</td>\n",
       "      <td>12.6</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet citation</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>342</th>\n",
       "      <td>30.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2385.0</td>\n",
       "      <td>12.9</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth reliant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343</th>\n",
       "      <td>39.1</td>\n",
       "      <td>4</td>\n",
       "      <td>79.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>1755.0</td>\n",
       "      <td>16.9</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota starlet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344</th>\n",
       "      <td>39.0</td>\n",
       "      <td>4</td>\n",
       "      <td>86.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>1875.0</td>\n",
       "      <td>16.4</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth champ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>345</th>\n",
       "      <td>35.1</td>\n",
       "      <td>4</td>\n",
       "      <td>81.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>1760.0</td>\n",
       "      <td>16.1</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>honda civic 1300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>346</th>\n",
       "      <td>32.3</td>\n",
       "      <td>4</td>\n",
       "      <td>97.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>2065.0</td>\n",
       "      <td>17.8</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>subaru</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>347</th>\n",
       "      <td>37.0</td>\n",
       "      <td>4</td>\n",
       "      <td>85.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>1975.0</td>\n",
       "      <td>19.4</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>datsun 210 mpg</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>348</th>\n",
       "      <td>37.7</td>\n",
       "      <td>4</td>\n",
       "      <td>89.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>2050.0</td>\n",
       "      <td>17.3</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota tercel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>349</th>\n",
       "      <td>34.1</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1985.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>mazda glc 4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350</th>\n",
       "      <td>34.7</td>\n",
       "      <td>4</td>\n",
       "      <td>105.0</td>\n",
       "      <td>63.0</td>\n",
       "      <td>2215.0</td>\n",
       "      <td>14.9</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth horizon 4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>351</th>\n",
       "      <td>34.4</td>\n",
       "      <td>4</td>\n",
       "      <td>98.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>2045.0</td>\n",
       "      <td>16.2</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>ford escort 4w</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>352</th>\n",
       "      <td>29.9</td>\n",
       "      <td>4</td>\n",
       "      <td>98.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>2380.0</td>\n",
       "      <td>20.7</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>ford escort 2h</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>353</th>\n",
       "      <td>33.0</td>\n",
       "      <td>4</td>\n",
       "      <td>105.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>2190.0</td>\n",
       "      <td>14.2</td>\n",
       "      <td>81</td>\n",
       "      <td>2</td>\n",
       "      <td>volkswagen jetta</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>355</th>\n",
       "      <td>33.7</td>\n",
       "      <td>4</td>\n",
       "      <td>107.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>2210.0</td>\n",
       "      <td>14.4</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>honda prelude</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>356</th>\n",
       "      <td>32.4</td>\n",
       "      <td>4</td>\n",
       "      <td>108.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>2350.0</td>\n",
       "      <td>16.8</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota corolla</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>357</th>\n",
       "      <td>32.9</td>\n",
       "      <td>4</td>\n",
       "      <td>119.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>2615.0</td>\n",
       "      <td>14.8</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>datsun 200sx</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>358</th>\n",
       "      <td>31.6</td>\n",
       "      <td>4</td>\n",
       "      <td>120.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>2635.0</td>\n",
       "      <td>18.3</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>mazda 626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>359</th>\n",
       "      <td>28.1</td>\n",
       "      <td>4</td>\n",
       "      <td>141.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>3230.0</td>\n",
       "      <td>20.4</td>\n",
       "      <td>81</td>\n",
       "      <td>2</td>\n",
       "      <td>peugeot 505s turbo diesel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>30.7</td>\n",
       "      <td>6</td>\n",
       "      <td>145.0</td>\n",
       "      <td>76.0</td>\n",
       "      <td>3160.0</td>\n",
       "      <td>19.6</td>\n",
       "      <td>81</td>\n",
       "      <td>2</td>\n",
       "      <td>volvo diesel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>361</th>\n",
       "      <td>25.4</td>\n",
       "      <td>6</td>\n",
       "      <td>168.0</td>\n",
       "      <td>116.0</td>\n",
       "      <td>2900.0</td>\n",
       "      <td>12.6</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota cressida</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>24.2</td>\n",
       "      <td>6</td>\n",
       "      <td>146.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>2930.0</td>\n",
       "      <td>13.8</td>\n",
       "      <td>81</td>\n",
       "      <td>3</td>\n",
       "      <td>datsun 810 maxima</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>22.4</td>\n",
       "      <td>6</td>\n",
       "      <td>231.0</td>\n",
       "      <td>110.0</td>\n",
       "      <td>3415.0</td>\n",
       "      <td>15.8</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>buick century</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>26.6</td>\n",
       "      <td>8</td>\n",
       "      <td>350.0</td>\n",
       "      <td>105.0</td>\n",
       "      <td>3725.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>oldsmobile cutlass ls</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>365</th>\n",
       "      <td>20.2</td>\n",
       "      <td>6</td>\n",
       "      <td>200.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>3060.0</td>\n",
       "      <td>17.1</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>ford granada gl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>366</th>\n",
       "      <td>17.6</td>\n",
       "      <td>6</td>\n",
       "      <td>225.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>3465.0</td>\n",
       "      <td>16.6</td>\n",
       "      <td>81</td>\n",
       "      <td>1</td>\n",
       "      <td>chrysler lebaron salon</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>112.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>2605.0</td>\n",
       "      <td>19.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet cavalier</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>112.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>2640.0</td>\n",
       "      <td>18.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet cavalier wagon</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>369</th>\n",
       "      <td>34.0</td>\n",
       "      <td>4</td>\n",
       "      <td>112.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>2395.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet cavalier 2-door</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>370</th>\n",
       "      <td>31.0</td>\n",
       "      <td>4</td>\n",
       "      <td>112.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>2575.0</td>\n",
       "      <td>16.2</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>pontiac j2000 se hatchback</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>371</th>\n",
       "      <td>29.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2525.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge aries se</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>372</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>151.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>2735.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>pontiac phoenix</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>373</th>\n",
       "      <td>24.0</td>\n",
       "      <td>4</td>\n",
       "      <td>140.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2865.0</td>\n",
       "      <td>16.4</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford fairmont futura</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>374</th>\n",
       "      <td>36.0</td>\n",
       "      <td>4</td>\n",
       "      <td>105.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>1980.0</td>\n",
       "      <td>15.3</td>\n",
       "      <td>82</td>\n",
       "      <td>2</td>\n",
       "      <td>volkswagen rabbit l</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>375</th>\n",
       "      <td>37.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>2025.0</td>\n",
       "      <td>18.2</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>mazda glc custom l</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376</th>\n",
       "      <td>31.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1970.0</td>\n",
       "      <td>17.6</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>mazda glc custom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>377</th>\n",
       "      <td>38.0</td>\n",
       "      <td>4</td>\n",
       "      <td>105.0</td>\n",
       "      <td>63.0</td>\n",
       "      <td>2125.0</td>\n",
       "      <td>14.7</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth horizon miser</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>378</th>\n",
       "      <td>36.0</td>\n",
       "      <td>4</td>\n",
       "      <td>98.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>2125.0</td>\n",
       "      <td>17.3</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>mercury lynx l</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>379</th>\n",
       "      <td>36.0</td>\n",
       "      <td>4</td>\n",
       "      <td>120.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>2160.0</td>\n",
       "      <td>14.5</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>nissan stanza xe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>380</th>\n",
       "      <td>36.0</td>\n",
       "      <td>4</td>\n",
       "      <td>107.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>2205.0</td>\n",
       "      <td>14.5</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>honda accord</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>381</th>\n",
       "      <td>34.0</td>\n",
       "      <td>4</td>\n",
       "      <td>108.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>2245.0</td>\n",
       "      <td>16.9</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota corolla</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>382</th>\n",
       "      <td>38.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>honda civic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>383</th>\n",
       "      <td>32.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>15.7</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>honda civic (auto)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>384</th>\n",
       "      <td>38.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>16.2</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>datsun 310 gx</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>385</th>\n",
       "      <td>25.0</td>\n",
       "      <td>6</td>\n",
       "      <td>181.0</td>\n",
       "      <td>110.0</td>\n",
       "      <td>2945.0</td>\n",
       "      <td>16.4</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>buick century limited</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>386</th>\n",
       "      <td>38.0</td>\n",
       "      <td>6</td>\n",
       "      <td>262.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>3015.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>oldsmobile cutlass ciera (diesel)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>387</th>\n",
       "      <td>26.0</td>\n",
       "      <td>4</td>\n",
       "      <td>156.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2585.0</td>\n",
       "      <td>14.5</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chrysler lebaron medallion</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388</th>\n",
       "      <td>22.0</td>\n",
       "      <td>6</td>\n",
       "      <td>232.0</td>\n",
       "      <td>112.0</td>\n",
       "      <td>2835.0</td>\n",
       "      <td>14.7</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford granada l</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>389</th>\n",
       "      <td>32.0</td>\n",
       "      <td>4</td>\n",
       "      <td>144.0</td>\n",
       "      <td>96.0</td>\n",
       "      <td>2665.0</td>\n",
       "      <td>13.9</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>toyota celica gt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>390</th>\n",
       "      <td>36.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2370.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge charger 2.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>391</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>151.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>2950.0</td>\n",
       "      <td>17.3</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet camaro</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>392</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>140.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>2790.0</td>\n",
       "      <td>15.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford mustang gl</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>393</th>\n",
       "      <td>44.0</td>\n",
       "      <td>4</td>\n",
       "      <td>97.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>2130.0</td>\n",
       "      <td>24.6</td>\n",
       "      <td>82</td>\n",
       "      <td>2</td>\n",
       "      <td>vw pickup</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>394</th>\n",
       "      <td>32.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2295.0</td>\n",
       "      <td>11.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge rampage</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>395</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>120.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>2625.0</td>\n",
       "      <td>18.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>ford ranger</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>396</th>\n",
       "      <td>31.0</td>\n",
       "      <td>4</td>\n",
       "      <td>119.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>2720.0</td>\n",
       "      <td>19.4</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "      <td>chevy s-10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      mpg  cylinders  displacement  horsepower  weight  acceleration  year  \\\n",
       "338  27.2          4         135.0        84.0  2490.0          15.7    81   \n",
       "339  26.6          4         151.0        84.0  2635.0          16.4    81   \n",
       "340  25.8          4         156.0        92.0  2620.0          14.4    81   \n",
       "341  23.5          6         173.0       110.0  2725.0          12.6    81   \n",
       "342  30.0          4         135.0        84.0  2385.0          12.9    81   \n",
       "343  39.1          4          79.0        58.0  1755.0          16.9    81   \n",
       "344  39.0          4          86.0        64.0  1875.0          16.4    81   \n",
       "345  35.1          4          81.0        60.0  1760.0          16.1    81   \n",
       "346  32.3          4          97.0        67.0  2065.0          17.8    81   \n",
       "347  37.0          4          85.0        65.0  1975.0          19.4    81   \n",
       "348  37.7          4          89.0        62.0  2050.0          17.3    81   \n",
       "349  34.1          4          91.0        68.0  1985.0          16.0    81   \n",
       "350  34.7          4         105.0        63.0  2215.0          14.9    81   \n",
       "351  34.4          4          98.0        65.0  2045.0          16.2    81   \n",
       "352  29.9          4          98.0        65.0  2380.0          20.7    81   \n",
       "353  33.0          4         105.0        74.0  2190.0          14.2    81   \n",
       "355  33.7          4         107.0        75.0  2210.0          14.4    81   \n",
       "356  32.4          4         108.0        75.0  2350.0          16.8    81   \n",
       "357  32.9          4         119.0       100.0  2615.0          14.8    81   \n",
       "358  31.6          4         120.0        74.0  2635.0          18.3    81   \n",
       "359  28.1          4         141.0        80.0  3230.0          20.4    81   \n",
       "360  30.7          6         145.0        76.0  3160.0          19.6    81   \n",
       "361  25.4          6         168.0       116.0  2900.0          12.6    81   \n",
       "362  24.2          6         146.0       120.0  2930.0          13.8    81   \n",
       "363  22.4          6         231.0       110.0  3415.0          15.8    81   \n",
       "364  26.6          8         350.0       105.0  3725.0          19.0    81   \n",
       "365  20.2          6         200.0        88.0  3060.0          17.1    81   \n",
       "366  17.6          6         225.0        85.0  3465.0          16.6    81   \n",
       "367  28.0          4         112.0        88.0  2605.0          19.6    82   \n",
       "368  27.0          4         112.0        88.0  2640.0          18.6    82   \n",
       "369  34.0          4         112.0        88.0  2395.0          18.0    82   \n",
       "370  31.0          4         112.0        85.0  2575.0          16.2    82   \n",
       "371  29.0          4         135.0        84.0  2525.0          16.0    82   \n",
       "372  27.0          4         151.0        90.0  2735.0          18.0    82   \n",
       "373  24.0          4         140.0        92.0  2865.0          16.4    82   \n",
       "374  36.0          4         105.0        74.0  1980.0          15.3    82   \n",
       "375  37.0          4          91.0        68.0  2025.0          18.2    82   \n",
       "376  31.0          4          91.0        68.0  1970.0          17.6    82   \n",
       "377  38.0          4         105.0        63.0  2125.0          14.7    82   \n",
       "378  36.0          4          98.0        70.0  2125.0          17.3    82   \n",
       "379  36.0          4         120.0        88.0  2160.0          14.5    82   \n",
       "380  36.0          4         107.0        75.0  2205.0          14.5    82   \n",
       "381  34.0          4         108.0        70.0  2245.0          16.9    82   \n",
       "382  38.0          4          91.0        67.0  1965.0          15.0    82   \n",
       "383  32.0          4          91.0        67.0  1965.0          15.7    82   \n",
       "384  38.0          4          91.0        67.0  1995.0          16.2    82   \n",
       "385  25.0          6         181.0       110.0  2945.0          16.4    82   \n",
       "386  38.0          6         262.0        85.0  3015.0          17.0    82   \n",
       "387  26.0          4         156.0        92.0  2585.0          14.5    82   \n",
       "388  22.0          6         232.0       112.0  2835.0          14.7    82   \n",
       "389  32.0          4         144.0        96.0  2665.0          13.9    82   \n",
       "390  36.0          4         135.0        84.0  2370.0          13.0    82   \n",
       "391  27.0          4         151.0        90.0  2950.0          17.3    82   \n",
       "392  27.0          4         140.0        86.0  2790.0          15.6    82   \n",
       "393  44.0          4          97.0        52.0  2130.0          24.6    82   \n",
       "394  32.0          4         135.0        84.0  2295.0          11.6    82   \n",
       "395  28.0          4         120.0        79.0  2625.0          18.6    82   \n",
       "396  31.0          4         119.0        82.0  2720.0          19.4    82   \n",
       "\n",
       "     origin                               name  \n",
       "338       1                   plymouth reliant  \n",
       "339       1                      buick skylark  \n",
       "340       1             dodge aries wagon (sw)  \n",
       "341       1                 chevrolet citation  \n",
       "342       1                   plymouth reliant  \n",
       "343       3                     toyota starlet  \n",
       "344       1                     plymouth champ  \n",
       "345       3                   honda civic 1300  \n",
       "346       3                             subaru  \n",
       "347       3                     datsun 210 mpg  \n",
       "348       3                      toyota tercel  \n",
       "349       3                        mazda glc 4  \n",
       "350       1                 plymouth horizon 4  \n",
       "351       1                     ford escort 4w  \n",
       "352       1                     ford escort 2h  \n",
       "353       2                   volkswagen jetta  \n",
       "355       3                      honda prelude  \n",
       "356       3                     toyota corolla  \n",
       "357       3                       datsun 200sx  \n",
       "358       3                          mazda 626  \n",
       "359       2          peugeot 505s turbo diesel  \n",
       "360       2                       volvo diesel  \n",
       "361       3                    toyota cressida  \n",
       "362       3                  datsun 810 maxima  \n",
       "363       1                      buick century  \n",
       "364       1              oldsmobile cutlass ls  \n",
       "365       1                    ford granada gl  \n",
       "366       1             chrysler lebaron salon  \n",
       "367       1                 chevrolet cavalier  \n",
       "368       1           chevrolet cavalier wagon  \n",
       "369       1          chevrolet cavalier 2-door  \n",
       "370       1         pontiac j2000 se hatchback  \n",
       "371       1                     dodge aries se  \n",
       "372       1                    pontiac phoenix  \n",
       "373       1               ford fairmont futura  \n",
       "374       2                volkswagen rabbit l  \n",
       "375       3                 mazda glc custom l  \n",
       "376       3                   mazda glc custom  \n",
       "377       1             plymouth horizon miser  \n",
       "378       1                     mercury lynx l  \n",
       "379       3                   nissan stanza xe  \n",
       "380       3                       honda accord  \n",
       "381       3                     toyota corolla  \n",
       "382       3                        honda civic  \n",
       "383       3                 honda civic (auto)  \n",
       "384       3                      datsun 310 gx  \n",
       "385       1              buick century limited  \n",
       "386       1  oldsmobile cutlass ciera (diesel)  \n",
       "387       1         chrysler lebaron medallion  \n",
       "388       1                     ford granada l  \n",
       "389       3                   toyota celica gt  \n",
       "390       1                  dodge charger 2.2  \n",
       "391       1                   chevrolet camaro  \n",
       "392       1                    ford mustang gl  \n",
       "393       2                          vw pickup  \n",
       "394       1                      dodge rampage  \n",
       "395       1                        ford ranger  \n",
       "396       1                         chevy s-10  "
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx_80 = Auto['year'] > 80\n",
    "Auto[idx_80]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a02221a2",
   "metadata": {},
   "source": [
    "However, if we pass  in a list of strings to the `[]` method, then we obtain a data frame containing the corresponding set of *columns*. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "66d174f1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.366746Z",
     "iopub.status.busy": "2023-08-21T03:36:08.366648Z",
     "iopub.status.idle": "2023-08-21T03:36:08.371142Z",
     "shell.execute_reply": "2023-08-21T03:36:08.370889Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>horsepower</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>130.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>165.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>140.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>392</th>\n",
       "      <td>27.0</td>\n",
       "      <td>86.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>393</th>\n",
       "      <td>44.0</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>394</th>\n",
       "      <td>32.0</td>\n",
       "      <td>84.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>395</th>\n",
       "      <td>28.0</td>\n",
       "      <td>79.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>396</th>\n",
       "      <td>31.0</td>\n",
       "      <td>82.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>392 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      mpg  horsepower\n",
       "0    18.0       130.0\n",
       "1    15.0       165.0\n",
       "2    18.0       150.0\n",
       "3    16.0       150.0\n",
       "4    17.0       140.0\n",
       "..    ...         ...\n",
       "392  27.0        86.0\n",
       "393  44.0        52.0\n",
       "394  32.0        84.0\n",
       "395  28.0        79.0\n",
       "396  31.0        82.0\n",
       "\n",
       "[392 rows x 2 columns]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto[['mpg', 'horsepower']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54bef6a3",
   "metadata": {},
   "source": [
    "Since we did not specify an *index* column when we loaded our data frame, the rows are labeled using integers\n",
    "0 to 396."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "52789c77",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.372548Z",
     "iopub.status.busy": "2023-08-21T03:36:08.372468Z",
     "iopub.status.idle": "2023-08-21T03:36:08.374676Z",
     "shell.execute_reply": "2023-08-21T03:36:08.374427Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,\n",
       "            ...\n",
       "            387, 388, 389, 390, 391, 392, 393, 394, 395, 396],\n",
       "           dtype='int64', length=392)"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto.index\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f5fcb26",
   "metadata": {},
   "source": [
    "We can use the\n",
    "`set_index()` method to re-name the rows using the contents of `Auto['name']`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "d83650bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.376096Z",
     "iopub.status.busy": "2023-08-21T03:36:08.376016Z",
     "iopub.status.idle": "2023-08-21T03:36:08.382023Z",
     "shell.execute_reply": "2023-08-21T03:36:08.381780Z"
    }
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></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>chevrolet chevelle malibu</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>3504.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick skylark 320</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350.0</td>\n",
       "      <td>165.0</td>\n",
       "      <td>3693.0</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth satellite</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>3436.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amc rebel sst</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>3433.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>3449.0</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang gl</th>\n",
       "      <td>27.0</td>\n",
       "      <td>4</td>\n",
       "      <td>140.0</td>\n",
       "      <td>86.0</td>\n",
       "      <td>2790.0</td>\n",
       "      <td>15.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>vw pickup</th>\n",
       "      <td>44.0</td>\n",
       "      <td>4</td>\n",
       "      <td>97.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>2130.0</td>\n",
       "      <td>24.6</td>\n",
       "      <td>82</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge rampage</th>\n",
       "      <td>32.0</td>\n",
       "      <td>4</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2295.0</td>\n",
       "      <td>11.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford ranger</th>\n",
       "      <td>28.0</td>\n",
       "      <td>4</td>\n",
       "      <td>120.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>2625.0</td>\n",
       "      <td>18.6</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevy s-10</th>\n",
       "      <td>31.0</td>\n",
       "      <td>4</td>\n",
       "      <td>119.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>2720.0</td>\n",
       "      <td>19.4</td>\n",
       "      <td>82</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>392 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            mpg  cylinders  displacement  horsepower  weight  \\\n",
       "name                                                                           \n",
       "chevrolet chevelle malibu  18.0          8         307.0       130.0  3504.0   \n",
       "buick skylark 320          15.0          8         350.0       165.0  3693.0   \n",
       "plymouth satellite         18.0          8         318.0       150.0  3436.0   \n",
       "amc rebel sst              16.0          8         304.0       150.0  3433.0   \n",
       "ford torino                17.0          8         302.0       140.0  3449.0   \n",
       "...                         ...        ...           ...         ...     ...   \n",
       "ford mustang gl            27.0          4         140.0        86.0  2790.0   \n",
       "vw pickup                  44.0          4          97.0        52.0  2130.0   \n",
       "dodge rampage              32.0          4         135.0        84.0  2295.0   \n",
       "ford ranger                28.0          4         120.0        79.0  2625.0   \n",
       "chevy s-10                 31.0          4         119.0        82.0  2720.0   \n",
       "\n",
       "                           acceleration  year  origin  \n",
       "name                                                   \n",
       "chevrolet chevelle malibu          12.0    70       1  \n",
       "buick skylark 320                  11.5    70       1  \n",
       "plymouth satellite                 11.0    70       1  \n",
       "amc rebel sst                      12.0    70       1  \n",
       "ford torino                        10.5    70       1  \n",
       "...                                 ...   ...     ...  \n",
       "ford mustang gl                    15.6    82       1  \n",
       "vw pickup                          24.6    82       2  \n",
       "dodge rampage                      11.6    82       1  \n",
       "ford ranger                        18.6    82       1  \n",
       "chevy s-10                         19.4    82       1  \n",
       "\n",
       "[392 rows x 8 columns]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re = Auto.set_index('name')\n",
    "Auto_re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "880d79d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.383342Z",
     "iopub.status.busy": "2023-08-21T03:36:08.383266Z",
     "iopub.status.idle": "2023-08-21T03:36:08.385348Z",
     "shell.execute_reply": "2023-08-21T03:36:08.385111Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',\n",
       "       'acceleration', 'year', 'origin'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.columns\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbee53b8",
   "metadata": {},
   "source": [
    "We see that the column `'name'` is no longer there.\n",
    " \n",
    "Now that the index has been set to `name`, we can  access rows of the data \n",
    "frame by `name` using the `{loc[]`} method of\n",
    "`Auto`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "c01f4095",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.386800Z",
     "iopub.status.busy": "2023-08-21T03:36:08.386719Z",
     "iopub.status.idle": "2023-08-21T03:36:08.390900Z",
     "shell.execute_reply": "2023-08-21T03:36:08.390675Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></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>amc rebel sst</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>3433.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>3449.0</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                mpg  cylinders  displacement  horsepower  weight  \\\n",
       "name                                                               \n",
       "amc rebel sst  16.0          8         304.0       150.0  3433.0   \n",
       "ford torino    17.0          8         302.0       140.0  3449.0   \n",
       "\n",
       "               acceleration  year  origin  \n",
       "name                                       \n",
       "amc rebel sst          12.0    70       1  \n",
       "ford torino            10.5    70       1  "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows = ['amc rebel sst', 'ford torino']\n",
    "Auto_re.loc[rows]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29688cab",
   "metadata": {},
   "source": [
    "As an alternative to using the index name, we could retrieve the 4th and 5th rows of `Auto` using the `{iloc[]`} method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "a4202eb8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.392405Z",
     "iopub.status.busy": "2023-08-21T03:36:08.392322Z",
     "iopub.status.idle": "2023-08-21T03:36:08.396509Z",
     "shell.execute_reply": "2023-08-21T03:36:08.396244Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>year</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></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>amc rebel sst</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>3433.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>3449.0</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                mpg  cylinders  displacement  horsepower  weight  \\\n",
       "name                                                               \n",
       "amc rebel sst  16.0          8         304.0       150.0  3433.0   \n",
       "ford torino    17.0          8         302.0       140.0  3449.0   \n",
       "\n",
       "               acceleration  year  origin  \n",
       "name                                       \n",
       "amc rebel sst          12.0    70       1  \n",
       "ford torino            10.5    70       1  "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.iloc[[3,4]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5427ede0",
   "metadata": {},
   "source": [
    "We can also use it to retrieve the 1st, 3rd and and 4th columns of `Auto_re`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "948b2d07",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.397917Z",
     "iopub.status.busy": "2023-08-21T03:36:08.397838Z",
     "iopub.status.idle": "2023-08-21T03:36:08.402447Z",
     "shell.execute_reply": "2023-08-21T03:36:08.402192Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>chevrolet chevelle malibu</th>\n",
       "      <td>18.0</td>\n",
       "      <td>307.0</td>\n",
       "      <td>130.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick skylark 320</th>\n",
       "      <td>15.0</td>\n",
       "      <td>350.0</td>\n",
       "      <td>165.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth satellite</th>\n",
       "      <td>18.0</td>\n",
       "      <td>318.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amc rebel sst</th>\n",
       "      <td>16.0</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino</th>\n",
       "      <td>17.0</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang gl</th>\n",
       "      <td>27.0</td>\n",
       "      <td>140.0</td>\n",
       "      <td>86.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>vw pickup</th>\n",
       "      <td>44.0</td>\n",
       "      <td>97.0</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge rampage</th>\n",
       "      <td>32.0</td>\n",
       "      <td>135.0</td>\n",
       "      <td>84.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford ranger</th>\n",
       "      <td>28.0</td>\n",
       "      <td>120.0</td>\n",
       "      <td>79.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevy s-10</th>\n",
       "      <td>31.0</td>\n",
       "      <td>119.0</td>\n",
       "      <td>82.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>392 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            mpg  displacement  horsepower\n",
       "name                                                     \n",
       "chevrolet chevelle malibu  18.0         307.0       130.0\n",
       "buick skylark 320          15.0         350.0       165.0\n",
       "plymouth satellite         18.0         318.0       150.0\n",
       "amc rebel sst              16.0         304.0       150.0\n",
       "ford torino                17.0         302.0       140.0\n",
       "...                         ...           ...         ...\n",
       "ford mustang gl            27.0         140.0        86.0\n",
       "vw pickup                  44.0          97.0        52.0\n",
       "dodge rampage              32.0         135.0        84.0\n",
       "ford ranger                28.0         120.0        79.0\n",
       "chevy s-10                 31.0         119.0        82.0\n",
       "\n",
       "[392 rows x 3 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.iloc[:,[0,2,3]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83d56eb",
   "metadata": {},
   "source": [
    "We can extract the 4th and 5th rows, as well as the 1st, 3rd and 4th columns, using\n",
    "a single call to `iloc[]`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "1cfdcc5c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.403773Z",
     "iopub.status.busy": "2023-08-21T03:36:08.403692Z",
     "iopub.status.idle": "2023-08-21T03:36:08.407036Z",
     "shell.execute_reply": "2023-08-21T03:36:08.406794Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>amc rebel sst</th>\n",
       "      <td>16.0</td>\n",
       "      <td>304.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino</th>\n",
       "      <td>17.0</td>\n",
       "      <td>302.0</td>\n",
       "      <td>140.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                mpg  displacement  horsepower\n",
       "name                                         \n",
       "amc rebel sst  16.0         304.0       150.0\n",
       "ford torino    17.0         302.0       140.0"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.iloc[[3,4],[0,2,3]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bde6514",
   "metadata": {},
   "source": [
    "Index entries need not be unique: there are several cars  in the data frame named `ford galaxie 500`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "fd9c5cda",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.408461Z",
     "iopub.status.busy": "2023-08-21T03:36:08.408387Z",
     "iopub.status.idle": "2023-08-21T03:36:08.411673Z",
     "shell.execute_reply": "2023-08-21T03:36:08.411429Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ford galaxie 500</th>\n",
       "      <td>15.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford galaxie 500</th>\n",
       "      <td>14.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford galaxie 500</th>\n",
       "      <td>14.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   mpg  origin\n",
       "name                          \n",
       "ford galaxie 500  15.0       1\n",
       "ford galaxie 500  14.0       1\n",
       "ford galaxie 500  14.0       1"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.loc['ford galaxie 500', ['mpg', 'origin']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d097282",
   "metadata": {},
   "source": [
    "### More on Selecting Rows and Columns\n",
    "Suppose now that we want to create a data frame consisting of the  `weight` and `origin`  of the subset of cars with \n",
    "`year` greater than 80 --- i.e. those built after 1980.\n",
    "To do this, we first create a Boolean array that indexes the rows.\n",
    "The `loc[]` method allows for Boolean entries as well as strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "6d431cb5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.413092Z",
     "iopub.status.busy": "2023-08-21T03:36:08.413016Z",
     "iopub.status.idle": "2023-08-21T03:36:08.418535Z",
     "shell.execute_reply": "2023-08-21T03:36:08.418303Z"
    },
    "lines_to_next_cell": 2
   },
   "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>weight</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>plymouth reliant</th>\n",
       "      <td>2490.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick skylark</th>\n",
       "      <td>2635.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge aries wagon (sw)</th>\n",
       "      <td>2620.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet citation</th>\n",
       "      <td>2725.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth reliant</th>\n",
       "      <td>2385.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota starlet</th>\n",
       "      <td>1755.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth champ</th>\n",
       "      <td>1875.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic 1300</th>\n",
       "      <td>1760.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>2065.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210 mpg</th>\n",
       "      <td>1975.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota tercel</th>\n",
       "      <td>2050.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc 4</th>\n",
       "      <td>1985.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon 4</th>\n",
       "      <td>2215.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 4w</th>\n",
       "      <td>2045.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 2h</th>\n",
       "      <td>2380.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen jetta</th>\n",
       "      <td>2190.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda prelude</th>\n",
       "      <td>2210.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2350.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 200sx</th>\n",
       "      <td>2615.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda 626</th>\n",
       "      <td>2635.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peugeot 505s turbo diesel</th>\n",
       "      <td>3230.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo diesel</th>\n",
       "      <td>3160.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota cressida</th>\n",
       "      <td>2900.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 810 maxima</th>\n",
       "      <td>2930.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick century</th>\n",
       "      <td>3415.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oldsmobile cutlass ls</th>\n",
       "      <td>3725.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada gl</th>\n",
       "      <td>3060.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chrysler lebaron salon</th>\n",
       "      <td>3465.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier</th>\n",
       "      <td>2605.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier wagon</th>\n",
       "      <td>2640.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier 2-door</th>\n",
       "      <td>2395.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pontiac j2000 se hatchback</th>\n",
       "      <td>2575.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge aries se</th>\n",
       "      <td>2525.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pontiac phoenix</th>\n",
       "      <td>2735.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont futura</th>\n",
       "      <td>2865.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen rabbit l</th>\n",
       "      <td>1980.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom l</th>\n",
       "      <td>2025.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom</th>\n",
       "      <td>1970.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon miser</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury lynx l</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan stanza xe</th>\n",
       "      <td>2160.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda accord</th>\n",
       "      <td>2205.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2245.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic (auto)</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 310 gx</th>\n",
       "      <td>1995.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick century limited</th>\n",
       "      <td>2945.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oldsmobile cutlass ciera (diesel)</th>\n",
       "      <td>3015.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chrysler lebaron medallion</th>\n",
       "      <td>2585.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada l</th>\n",
       "      <td>2835.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota celica gt</th>\n",
       "      <td>2665.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge charger 2.2</th>\n",
       "      <td>2370.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet camaro</th>\n",
       "      <td>2950.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang gl</th>\n",
       "      <td>2790.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>vw pickup</th>\n",
       "      <td>2130.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge rampage</th>\n",
       "      <td>2295.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford ranger</th>\n",
       "      <td>2625.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevy s-10</th>\n",
       "      <td>2720.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   weight  origin\n",
       "name                                             \n",
       "plymouth reliant                   2490.0       1\n",
       "buick skylark                      2635.0       1\n",
       "dodge aries wagon (sw)             2620.0       1\n",
       "chevrolet citation                 2725.0       1\n",
       "plymouth reliant                   2385.0       1\n",
       "toyota starlet                     1755.0       3\n",
       "plymouth champ                     1875.0       1\n",
       "honda civic 1300                   1760.0       3\n",
       "subaru                             2065.0       3\n",
       "datsun 210 mpg                     1975.0       3\n",
       "toyota tercel                      2050.0       3\n",
       "mazda glc 4                        1985.0       3\n",
       "plymouth horizon 4                 2215.0       1\n",
       "ford escort 4w                     2045.0       1\n",
       "ford escort 2h                     2380.0       1\n",
       "volkswagen jetta                   2190.0       2\n",
       "honda prelude                      2210.0       3\n",
       "toyota corolla                     2350.0       3\n",
       "datsun 200sx                       2615.0       3\n",
       "mazda 626                          2635.0       3\n",
       "peugeot 505s turbo diesel          3230.0       2\n",
       "volvo diesel                       3160.0       2\n",
       "toyota cressida                    2900.0       3\n",
       "datsun 810 maxima                  2930.0       3\n",
       "buick century                      3415.0       1\n",
       "oldsmobile cutlass ls              3725.0       1\n",
       "ford granada gl                    3060.0       1\n",
       "chrysler lebaron salon             3465.0       1\n",
       "chevrolet cavalier                 2605.0       1\n",
       "chevrolet cavalier wagon           2640.0       1\n",
       "chevrolet cavalier 2-door          2395.0       1\n",
       "pontiac j2000 se hatchback         2575.0       1\n",
       "dodge aries se                     2525.0       1\n",
       "pontiac phoenix                    2735.0       1\n",
       "ford fairmont futura               2865.0       1\n",
       "volkswagen rabbit l                1980.0       2\n",
       "mazda glc custom l                 2025.0       3\n",
       "mazda glc custom                   1970.0       3\n",
       "plymouth horizon miser             2125.0       1\n",
       "mercury lynx l                     2125.0       1\n",
       "nissan stanza xe                   2160.0       3\n",
       "honda accord                       2205.0       3\n",
       "toyota corolla                     2245.0       3\n",
       "honda civic                        1965.0       3\n",
       "honda civic (auto)                 1965.0       3\n",
       "datsun 310 gx                      1995.0       3\n",
       "buick century limited              2945.0       1\n",
       "oldsmobile cutlass ciera (diesel)  3015.0       1\n",
       "chrysler lebaron medallion         2585.0       1\n",
       "ford granada l                     2835.0       1\n",
       "toyota celica gt                   2665.0       3\n",
       "dodge charger 2.2                  2370.0       1\n",
       "chevrolet camaro                   2950.0       1\n",
       "ford mustang gl                    2790.0       1\n",
       "vw pickup                          2130.0       2\n",
       "dodge rampage                      2295.0       1\n",
       "ford ranger                        2625.0       1\n",
       "chevy s-10                         2720.0       1"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx_80 = Auto_re['year'] > 80\n",
    "Auto_re.loc[idx_80, ['weight', 'origin']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "838a03e0",
   "metadata": {},
   "source": [
    "To do this more concisely, we can use an anonymous function called a `lambda`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "fac41ce1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.419882Z",
     "iopub.status.busy": "2023-08-21T03:36:08.419806Z",
     "iopub.status.idle": "2023-08-21T03:36:08.425319Z",
     "shell.execute_reply": "2023-08-21T03:36:08.425073Z"
    },
    "lines_to_next_cell": 0
   },
   "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>weight</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>plymouth reliant</th>\n",
       "      <td>2490.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick skylark</th>\n",
       "      <td>2635.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge aries wagon (sw)</th>\n",
       "      <td>2620.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet citation</th>\n",
       "      <td>2725.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth reliant</th>\n",
       "      <td>2385.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota starlet</th>\n",
       "      <td>1755.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth champ</th>\n",
       "      <td>1875.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic 1300</th>\n",
       "      <td>1760.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>2065.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210 mpg</th>\n",
       "      <td>1975.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota tercel</th>\n",
       "      <td>2050.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc 4</th>\n",
       "      <td>1985.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon 4</th>\n",
       "      <td>2215.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 4w</th>\n",
       "      <td>2045.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 2h</th>\n",
       "      <td>2380.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen jetta</th>\n",
       "      <td>2190.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda prelude</th>\n",
       "      <td>2210.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2350.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 200sx</th>\n",
       "      <td>2615.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda 626</th>\n",
       "      <td>2635.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>peugeot 505s turbo diesel</th>\n",
       "      <td>3230.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo diesel</th>\n",
       "      <td>3160.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota cressida</th>\n",
       "      <td>2900.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 810 maxima</th>\n",
       "      <td>2930.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick century</th>\n",
       "      <td>3415.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oldsmobile cutlass ls</th>\n",
       "      <td>3725.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada gl</th>\n",
       "      <td>3060.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chrysler lebaron salon</th>\n",
       "      <td>3465.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier</th>\n",
       "      <td>2605.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier wagon</th>\n",
       "      <td>2640.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier 2-door</th>\n",
       "      <td>2395.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pontiac j2000 se hatchback</th>\n",
       "      <td>2575.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge aries se</th>\n",
       "      <td>2525.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pontiac phoenix</th>\n",
       "      <td>2735.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont futura</th>\n",
       "      <td>2865.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen rabbit l</th>\n",
       "      <td>1980.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom l</th>\n",
       "      <td>2025.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom</th>\n",
       "      <td>1970.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon miser</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury lynx l</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan stanza xe</th>\n",
       "      <td>2160.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda accord</th>\n",
       "      <td>2205.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2245.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic (auto)</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 310 gx</th>\n",
       "      <td>1995.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buick century limited</th>\n",
       "      <td>2945.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oldsmobile cutlass ciera (diesel)</th>\n",
       "      <td>3015.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chrysler lebaron medallion</th>\n",
       "      <td>2585.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada l</th>\n",
       "      <td>2835.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota celica gt</th>\n",
       "      <td>2665.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge charger 2.2</th>\n",
       "      <td>2370.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet camaro</th>\n",
       "      <td>2950.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang gl</th>\n",
       "      <td>2790.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>vw pickup</th>\n",
       "      <td>2130.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge rampage</th>\n",
       "      <td>2295.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford ranger</th>\n",
       "      <td>2625.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevy s-10</th>\n",
       "      <td>2720.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   weight  origin\n",
       "name                                             \n",
       "plymouth reliant                   2490.0       1\n",
       "buick skylark                      2635.0       1\n",
       "dodge aries wagon (sw)             2620.0       1\n",
       "chevrolet citation                 2725.0       1\n",
       "plymouth reliant                   2385.0       1\n",
       "toyota starlet                     1755.0       3\n",
       "plymouth champ                     1875.0       1\n",
       "honda civic 1300                   1760.0       3\n",
       "subaru                             2065.0       3\n",
       "datsun 210 mpg                     1975.0       3\n",
       "toyota tercel                      2050.0       3\n",
       "mazda glc 4                        1985.0       3\n",
       "plymouth horizon 4                 2215.0       1\n",
       "ford escort 4w                     2045.0       1\n",
       "ford escort 2h                     2380.0       1\n",
       "volkswagen jetta                   2190.0       2\n",
       "honda prelude                      2210.0       3\n",
       "toyota corolla                     2350.0       3\n",
       "datsun 200sx                       2615.0       3\n",
       "mazda 626                          2635.0       3\n",
       "peugeot 505s turbo diesel          3230.0       2\n",
       "volvo diesel                       3160.0       2\n",
       "toyota cressida                    2900.0       3\n",
       "datsun 810 maxima                  2930.0       3\n",
       "buick century                      3415.0       1\n",
       "oldsmobile cutlass ls              3725.0       1\n",
       "ford granada gl                    3060.0       1\n",
       "chrysler lebaron salon             3465.0       1\n",
       "chevrolet cavalier                 2605.0       1\n",
       "chevrolet cavalier wagon           2640.0       1\n",
       "chevrolet cavalier 2-door          2395.0       1\n",
       "pontiac j2000 se hatchback         2575.0       1\n",
       "dodge aries se                     2525.0       1\n",
       "pontiac phoenix                    2735.0       1\n",
       "ford fairmont futura               2865.0       1\n",
       "volkswagen rabbit l                1980.0       2\n",
       "mazda glc custom l                 2025.0       3\n",
       "mazda glc custom                   1970.0       3\n",
       "plymouth horizon miser             2125.0       1\n",
       "mercury lynx l                     2125.0       1\n",
       "nissan stanza xe                   2160.0       3\n",
       "honda accord                       2205.0       3\n",
       "toyota corolla                     2245.0       3\n",
       "honda civic                        1965.0       3\n",
       "honda civic (auto)                 1965.0       3\n",
       "datsun 310 gx                      1995.0       3\n",
       "buick century limited              2945.0       1\n",
       "oldsmobile cutlass ciera (diesel)  3015.0       1\n",
       "chrysler lebaron medallion         2585.0       1\n",
       "ford granada l                     2835.0       1\n",
       "toyota celica gt                   2665.0       3\n",
       "dodge charger 2.2                  2370.0       1\n",
       "chevrolet camaro                   2950.0       1\n",
       "ford mustang gl                    2790.0       1\n",
       "vw pickup                          2130.0       2\n",
       "dodge rampage                      2295.0       1\n",
       "ford ranger                        2625.0       1\n",
       "chevy s-10                         2720.0       1"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.loc[lambda df: df['year'] > 80, ['weight', 'origin']]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08e61254",
   "metadata": {},
   "source": [
    "The `lambda` call creates a function that takes a single\n",
    "argument, here `df`, and returns `df['year']>80`.\n",
    "Since it is created inside the `loc[]` method for the\n",
    "dataframe `Auto_re`, that dataframe will be the argument supplied.\n",
    "As another example of using a `lambda`, suppose that\n",
    "we want all cars built after 1980 that achieve greater than 30 miles per gallon:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "b0885654",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.426701Z",
     "iopub.status.busy": "2023-08-21T03:36:08.426621Z",
     "iopub.status.idle": "2023-08-21T03:36:08.431378Z",
     "shell.execute_reply": "2023-08-21T03:36:08.431148Z"
    },
    "lines_to_next_cell": 0
   },
   "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>weight</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>toyota starlet</th>\n",
       "      <td>1755.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth champ</th>\n",
       "      <td>1875.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic 1300</th>\n",
       "      <td>1760.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>subaru</th>\n",
       "      <td>2065.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210 mpg</th>\n",
       "      <td>1975.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota tercel</th>\n",
       "      <td>2050.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc 4</th>\n",
       "      <td>1985.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon 4</th>\n",
       "      <td>2215.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 4w</th>\n",
       "      <td>2045.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen jetta</th>\n",
       "      <td>2190.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda prelude</th>\n",
       "      <td>2210.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2350.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 200sx</th>\n",
       "      <td>2615.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda 626</th>\n",
       "      <td>2635.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volvo diesel</th>\n",
       "      <td>3160.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevrolet cavalier 2-door</th>\n",
       "      <td>2395.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pontiac j2000 se hatchback</th>\n",
       "      <td>2575.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>volkswagen rabbit l</th>\n",
       "      <td>1980.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom l</th>\n",
       "      <td>2025.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mazda glc custom</th>\n",
       "      <td>1970.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>plymouth horizon miser</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mercury lynx l</th>\n",
       "      <td>2125.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>nissan stanza xe</th>\n",
       "      <td>2160.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda accord</th>\n",
       "      <td>2205.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota corolla</th>\n",
       "      <td>2245.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>honda civic (auto)</th>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 310 gx</th>\n",
       "      <td>1995.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>oldsmobile cutlass ciera (diesel)</th>\n",
       "      <td>3015.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>toyota celica gt</th>\n",
       "      <td>2665.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge charger 2.2</th>\n",
       "      <td>2370.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>vw pickup</th>\n",
       "      <td>2130.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dodge rampage</th>\n",
       "      <td>2295.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chevy s-10</th>\n",
       "      <td>2720.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   weight  origin\n",
       "name                                             \n",
       "toyota starlet                     1755.0       3\n",
       "plymouth champ                     1875.0       1\n",
       "honda civic 1300                   1760.0       3\n",
       "subaru                             2065.0       3\n",
       "datsun 210 mpg                     1975.0       3\n",
       "toyota tercel                      2050.0       3\n",
       "mazda glc 4                        1985.0       3\n",
       "plymouth horizon 4                 2215.0       1\n",
       "ford escort 4w                     2045.0       1\n",
       "volkswagen jetta                   2190.0       2\n",
       "honda prelude                      2210.0       3\n",
       "toyota corolla                     2350.0       3\n",
       "datsun 200sx                       2615.0       3\n",
       "mazda 626                          2635.0       3\n",
       "volvo diesel                       3160.0       2\n",
       "chevrolet cavalier 2-door          2395.0       1\n",
       "pontiac j2000 se hatchback         2575.0       1\n",
       "volkswagen rabbit l                1980.0       2\n",
       "mazda glc custom l                 2025.0       3\n",
       "mazda glc custom                   1970.0       3\n",
       "plymouth horizon miser             2125.0       1\n",
       "mercury lynx l                     2125.0       1\n",
       "nissan stanza xe                   2160.0       3\n",
       "honda accord                       2205.0       3\n",
       "toyota corolla                     2245.0       3\n",
       "honda civic                        1965.0       3\n",
       "honda civic (auto)                 1965.0       3\n",
       "datsun 310 gx                      1995.0       3\n",
       "oldsmobile cutlass ciera (diesel)  3015.0       1\n",
       "toyota celica gt                   2665.0       3\n",
       "dodge charger 2.2                  2370.0       1\n",
       "vw pickup                          2130.0       2\n",
       "dodge rampage                      2295.0       1\n",
       "chevy s-10                         2720.0       1"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.loc[lambda df: (df['year'] > 80) & (df['mpg'] > 30),\n",
    "            ['weight', 'origin']\n",
    "           ]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d87fc459",
   "metadata": {},
   "source": [
    "The symbol `&` computes an element-wise *and* operation.\n",
    "As another example, suppose that we want to retrieve all `Ford` and `Datsun`\n",
    "cars with `displacement` less than 300. We check whether each `name` entry contains either the string `ford` or `datsun` using the  `str.contains()` method of the `index` attribute of \n",
    "of the dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "213945a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.432780Z",
     "iopub.status.busy": "2023-08-21T03:36:08.432696Z",
     "iopub.status.idle": "2023-08-21T03:36:08.438473Z",
     "shell.execute_reply": "2023-08-21T03:36:08.438250Z"
    },
    "lines_to_next_cell": 0
   },
   "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>weight</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ford maverick</th>\n",
       "      <td>2587.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun pl510</th>\n",
       "      <td>2130.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun pl510</th>\n",
       "      <td>2130.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford torino 500</th>\n",
       "      <td>3302.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang</th>\n",
       "      <td>3139.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 1200</th>\n",
       "      <td>1613.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto runabout</th>\n",
       "      <td>2226.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto (sw)</th>\n",
       "      <td>2395.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 510 (sw)</th>\n",
       "      <td>2288.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford maverick</th>\n",
       "      <td>3021.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 610</th>\n",
       "      <td>2379.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto</th>\n",
       "      <td>2310.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun b210</th>\n",
       "      <td>1950.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto</th>\n",
       "      <td>2451.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 710</th>\n",
       "      <td>2003.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford maverick</th>\n",
       "      <td>3158.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto</th>\n",
       "      <td>2639.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 710</th>\n",
       "      <td>2545.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto</th>\n",
       "      <td>2984.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford maverick</th>\n",
       "      <td>3012.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada ghia</th>\n",
       "      <td>3574.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun b-210</th>\n",
       "      <td>1990.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford pinto</th>\n",
       "      <td>2565.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun f-10 hatchback</th>\n",
       "      <td>1945.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada</th>\n",
       "      <td>3525.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang ii 2+2</th>\n",
       "      <td>2755.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 810</th>\n",
       "      <td>2815.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fiesta</th>\n",
       "      <td>1800.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun b210 gx</th>\n",
       "      <td>2070.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont (auto)</th>\n",
       "      <td>2965.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont (man)</th>\n",
       "      <td>2720.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 510</th>\n",
       "      <td>2300.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 200-sx</th>\n",
       "      <td>2405.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont 4</th>\n",
       "      <td>2890.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210</th>\n",
       "      <td>2020.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 310</th>\n",
       "      <td>2019.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont</th>\n",
       "      <td>2870.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 510 hatchback</th>\n",
       "      <td>2434.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210</th>\n",
       "      <td>2110.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 280-zx</th>\n",
       "      <td>2910.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 210 mpg</th>\n",
       "      <td>1975.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 4w</th>\n",
       "      <td>2045.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford escort 2h</th>\n",
       "      <td>2380.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 200sx</th>\n",
       "      <td>2615.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 810 maxima</th>\n",
       "      <td>2930.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada gl</th>\n",
       "      <td>3060.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford fairmont futura</th>\n",
       "      <td>2865.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datsun 310 gx</th>\n",
       "      <td>1995.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford granada l</th>\n",
       "      <td>2835.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford mustang gl</th>\n",
       "      <td>2790.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ford ranger</th>\n",
       "      <td>2625.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       weight  origin\n",
       "name                                 \n",
       "ford maverick          2587.0       1\n",
       "datsun pl510           2130.0       3\n",
       "datsun pl510           2130.0       3\n",
       "ford torino 500        3302.0       1\n",
       "ford mustang           3139.0       1\n",
       "datsun 1200            1613.0       3\n",
       "ford pinto runabout    2226.0       1\n",
       "ford pinto (sw)        2395.0       1\n",
       "datsun 510 (sw)        2288.0       3\n",
       "ford maverick          3021.0       1\n",
       "datsun 610             2379.0       3\n",
       "ford pinto             2310.0       1\n",
       "datsun b210            1950.0       3\n",
       "ford pinto             2451.0       1\n",
       "datsun 710             2003.0       3\n",
       "ford maverick          3158.0       1\n",
       "ford pinto             2639.0       1\n",
       "datsun 710             2545.0       3\n",
       "ford pinto             2984.0       1\n",
       "ford maverick          3012.0       1\n",
       "ford granada ghia      3574.0       1\n",
       "datsun b-210           1990.0       3\n",
       "ford pinto             2565.0       1\n",
       "datsun f-10 hatchback  1945.0       3\n",
       "ford granada           3525.0       1\n",
       "ford mustang ii 2+2    2755.0       1\n",
       "datsun 810             2815.0       3\n",
       "ford fiesta            1800.0       1\n",
       "datsun b210 gx         2070.0       3\n",
       "ford fairmont (auto)   2965.0       1\n",
       "ford fairmont (man)    2720.0       1\n",
       "datsun 510             2300.0       3\n",
       "datsun 200-sx          2405.0       3\n",
       "ford fairmont 4        2890.0       1\n",
       "datsun 210             2020.0       3\n",
       "datsun 310             2019.0       3\n",
       "ford fairmont          2870.0       1\n",
       "datsun 510 hatchback   2434.0       3\n",
       "datsun 210             2110.0       3\n",
       "datsun 280-zx          2910.0       3\n",
       "datsun 210 mpg         1975.0       3\n",
       "ford escort 4w         2045.0       1\n",
       "ford escort 2h         2380.0       1\n",
       "datsun 200sx           2615.0       3\n",
       "datsun 810 maxima      2930.0       3\n",
       "ford granada gl        3060.0       1\n",
       "ford fairmont futura   2865.0       1\n",
       "datsun 310 gx          1995.0       3\n",
       "ford granada l         2835.0       1\n",
       "ford mustang gl        2790.0       1\n",
       "ford ranger            2625.0       1"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto_re.loc[lambda df: (df['displacement'] < 300)\n",
    "                       & (df.index.str.contains('ford')\n",
    "                       | df.index.str.contains('datsun')),\n",
    "            ['weight', 'origin']\n",
    "           ]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a940fd1",
   "metadata": {},
   "source": [
    "Here, the symbol `|` computes an element-wise *or* operation.\n",
    " \n",
    "In summary, a powerful set of operations is available to index the rows and columns of data frames. For integer based queries, use the `iloc[]` method. For string and Boolean\n",
    "selections, use the `loc[]` method. For functional queries that filter rows, use the `loc[]` method\n",
    "with a function (typically a `lambda`) in the rows argument.\n",
    "\n",
    "## For Loops\n",
    "A `for` loop is a standard tool in many languages that\n",
    "repeatedly evaluates some chunk of code while\n",
    "varying different values inside the code.\n",
    "For example, suppose we loop over elements of a list and compute their sum."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "a3c4060a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.439902Z",
     "iopub.status.busy": "2023-08-21T03:36:08.439825Z",
     "iopub.status.idle": "2023-08-21T03:36:08.441707Z",
     "shell.execute_reply": "2023-08-21T03:36:08.441464Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total is: 24\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "for value in [3,2,19]:\n",
    "    total += value\n",
    "print('Total is: {0}'.format(total))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9117e3a1",
   "metadata": {},
   "source": [
    "The indented code beneath the line with the `for` statement is run\n",
    "for each value in the sequence\n",
    "specified in the `for` statement. The loop ends either\n",
    "when the cell ends or when code is indented at the same level\n",
    "as the original `for` statement.\n",
    "We see that the final line above which prints the total is executed\n",
    "only once after the for loop has terminated. Loops\n",
    "can be nested by additional indentation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "f2bffb69",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.443349Z",
     "iopub.status.busy": "2023-08-21T03:36:08.443241Z",
     "iopub.status.idle": "2023-08-21T03:36:08.445173Z",
     "shell.execute_reply": "2023-08-21T03:36:08.444935Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total is: 144\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "for value in [2,3,19]:\n",
    "    for weight in [3, 2, 1]:\n",
    "        total += value * weight\n",
    "print('Total is: {0}'.format(total))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f99e85b",
   "metadata": {},
   "source": [
    "Above, we summed over each combination of `value` and `weight`.\n",
    "We also took advantage of the *increment* notation\n",
    "in `Python`: the expression `a += b` is equivalent\n",
    "to `a = a + b`. Besides\n",
    "being a convenient notation, this can save time in computationally\n",
    "heavy tasks in which the intermediate value of `a+b` need not\n",
    "be explicitly created.\n",
    "\n",
    "Perhaps a more\n",
    "common task would be to sum over `(value, weight)` pairs. For instance,\n",
    "to compute the average value of a random variable that takes on\n",
    "possible values 2, 3 or 19 with probability 0.2, 0.3, 0.5 respectively\n",
    "we would compute the weighted sum. Tasks such as this\n",
    "can often be accomplished using the `zip()`  function that\n",
    "loops over a sequence of tuples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "ee827a53",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.446610Z",
     "iopub.status.busy": "2023-08-21T03:36:08.446520Z",
     "iopub.status.idle": "2023-08-21T03:36:08.448323Z",
     "shell.execute_reply": "2023-08-21T03:36:08.448070Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weighted average is: 10.8\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "for value, weight in zip([2,3,19],\n",
    "                         [0.2,0.3,0.5]):\n",
    "    total += weight * value\n",
    "print('Weighted average is: {0}'.format(total))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dec18466",
   "metadata": {},
   "source": [
    "### String Formatting\n",
    "In the code chunk above we also printed a string\n",
    "displaying the total. However, the object `total`\n",
    "is an  integer and not a string.\n",
    "Inserting the value of something into\n",
    "a string is a common task, made\n",
    "simple using\n",
    "some of the powerful string formatting\n",
    "tools in `Python`.\n",
    "Many data cleaning tasks involve\n",
    "manipulating and programmatically\n",
    "producing strings.\n",
    "\n",
    "For example we may want to loop over the columns of a data frame and\n",
    "print the percent missing in each column.\n",
    "Let’s create a data frame `D` with columns in which 20% of the entries are missing i.e. set\n",
    "to `np.nan`.  We’ll create the\n",
    "values in `D` from a normal distribution with mean 0 and variance 1 using `rng.standard_normal()`\n",
    "and then overwrite some random entries using `rng.choice()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "3a097fbc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.449714Z",
     "iopub.status.busy": "2023-08-21T03:36:08.449623Z",
     "iopub.status.idle": "2023-08-21T03:36:08.454097Z",
     "shell.execute_reply": "2023-08-21T03:36:08.453813Z"
    },
    "lines_to_next_cell": 2
   },
   "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>food</th>\n",
       "      <th>bar</th>\n",
       "      <th>pickle</th>\n",
       "      <th>snack</th>\n",
       "      <th>popcorn</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.345584</td>\n",
       "      <td>0.821618</td>\n",
       "      <td>0.330437</td>\n",
       "      <td>-1.303157</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.536953</td>\n",
       "      <td>0.581118</td>\n",
       "      <td>0.364572</td>\n",
       "      <td>0.294132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.546713</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.162910</td>\n",
       "      <td>-0.482119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       food       bar    pickle     snack   popcorn\n",
       "0  0.345584  0.821618  0.330437 -1.303157       NaN\n",
       "1       NaN -0.536953  0.581118  0.364572  0.294132\n",
       "2       NaN  0.546713       NaN -0.162910 -0.482119"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = np.random.default_rng(1)\n",
    "A = rng.standard_normal((127, 5))\n",
    "M = rng.choice([0, np.nan], p=[0.8,0.2], size=A.shape)\n",
    "A += M\n",
    "D = pd.DataFrame(A, columns=['food',\n",
    "                             'bar',\n",
    "                             'pickle',\n",
    "                             'snack',\n",
    "                             'popcorn'])\n",
    "D[:3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "e064e170",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.455528Z",
     "iopub.status.busy": "2023-08-21T03:36:08.455447Z",
     "iopub.status.idle": "2023-08-21T03:36:08.457783Z",
     "shell.execute_reply": "2023-08-21T03:36:08.457543Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Column \"food\" has 16.54% missing values\n",
      "Column \"bar\" has 25.98% missing values\n",
      "Column \"pickle\" has 29.13% missing values\n",
      "Column \"snack\" has 21.26% missing values\n",
      "Column \"popcorn\" has 22.83% missing values\n"
     ]
    }
   ],
   "source": [
    "for col in D.columns:\n",
    "    template = 'Column \"{0}\" has {1:.2%} missing values'\n",
    "    print(template.format(col,\n",
    "          np.isnan(D[col]).mean()))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a3e4dd8",
   "metadata": {},
   "source": [
    "We see that the `template.format()` method expects two arguments `{0}`\n",
    "and `{1:.2%}`, and the latter includes some formatting\n",
    "information. In particular, it specifies that the second argument should be expressed as a percent with two decimal digits.\n",
    "\n",
    "The reference\n",
    "[docs.python.org/3/library/string.html](https://docs.python.org/3/library/string.html)\n",
    "includes many helpful and more complex examples."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8fd496a",
   "metadata": {},
   "source": [
    "## Additional Graphical and Numerical Summaries\n",
    "We can use the `ax.plot()` or  `ax.scatter()`  functions to display the quantitative variables. However, simply typing the variable names will produce an error message,\n",
    "because `Python` does not know to look in the  `Auto`  data set for those variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "c915ca52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.459248Z",
     "iopub.status.busy": "2023-08-21T03:36:08.459155Z",
     "iopub.status.idle": "2023-08-21T03:36:08.552220Z",
     "shell.execute_reply": "2023-08-21T03:36:08.551914Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'horsepower' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[102], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m fig, ax \u001b[38;5;241m=\u001b[39m subplots(figsize\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m8\u001b[39m, \u001b[38;5;241m8\u001b[39m))\n\u001b[0;32m----> 2\u001b[0m ax\u001b[38;5;241m.\u001b[39mplot(\u001b[43mhorsepower\u001b[49m, mpg, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mo\u001b[39m\u001b[38;5;124m'\u001b[39m);\n",
      "\u001b[0;31mNameError\u001b[0m: name 'horsepower' is not defined"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12036 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.plot(horsepower, mpg, 'o');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63d47021",
   "metadata": {},
   "source": [
    "We can address this by accessing the columns directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "65cd6d02",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.553877Z",
     "iopub.status.busy": "2023-08-21T03:36:08.553770Z",
     "iopub.status.idle": "2023-08-21T03:36:08.640715Z",
     "shell.execute_reply": "2023-08-21T03:36:08.640411Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 42536 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "ax.plot(Auto['horsepower'], Auto['mpg'], 'o');\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "726836f0",
   "metadata": {},
   "source": [
    "Alternatively, we can use the `plot()` method with the call `Auto.plot()`.\n",
    "Using this method,\n",
    "the variables  can be accessed by name.\n",
    "The plot methods of a data frame return a familiar object:\n",
    "an axes. We can use it to update the plot as we did previously: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "76b5c0b1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.642311Z",
     "iopub.status.busy": "2023-08-21T03:36:08.642208Z",
     "iopub.status.idle": "2023-08-21T03:36:08.737860Z",
     "shell.execute_reply": "2023-08-21T03:36:08.737565Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38776 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = Auto.plot.scatter('horsepower', 'mpg')\n",
    "ax.set_title('Horsepower vs. MPG');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69c46251",
   "metadata": {},
   "source": [
    "If we want to save\n",
    "the figure that contains a given axes, we can find the relevant figure\n",
    "by accessing the `figure` attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "183a2c2b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.739490Z",
     "iopub.status.busy": "2023-08-21T03:36:08.739374Z",
     "iopub.status.idle": "2023-08-21T03:36:08.779690Z",
     "shell.execute_reply": "2023-08-21T03:36:08.779420Z"
    }
   },
   "outputs": [],
   "source": [
    "fig = ax.figure\n",
    "fig.savefig('horsepower_mpg.png');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f10cb46",
   "metadata": {},
   "source": [
    "We can further instruct the data frame to plot to a particular axes object. In this\n",
    "case the corresponding `plot()` method will return the\n",
    "modified axes we passed in as an argument. Note that\n",
    "when we request a one-dimensional grid of plots, the object `axes` is similarly\n",
    "one-dimensional. We place our scatter plot in the middle plot of a row of three plots\n",
    "within a figure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "75fbb981",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.781309Z",
     "iopub.status.busy": "2023-08-21T03:36:08.781225Z",
     "iopub.status.idle": "2023-08-21T03:36:08.949186Z",
     "shell.execute_reply": "2023-08-21T03:36:08.948871Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47232 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = subplots(ncols=3, figsize=(15, 5))\n",
    "Auto.plot.scatter('horsepower', 'mpg', ax=axes[1]);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ffc0da",
   "metadata": {},
   "source": [
    "Note also that the columns of a data frame can be accessed as attributes: try typing in `Auto.horsepower`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c4705e0",
   "metadata": {},
   "source": [
    "We now consider the `cylinders` variable. Typing in `Auto.cylinders.dtype` reveals that it is being treated as a quantitative variable. \n",
    "However, since there is only a small number of possible values for this variable, we may wish to treat it as \n",
    " qualitative.  Below, we replace\n",
    "the `cylinders` column with a categorical version of `Auto.cylinders`. The function `pd.Series()`  owes its name to the fact that `pandas` is often used in time series applications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "55b3a1cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.950967Z",
     "iopub.status.busy": "2023-08-21T03:36:08.950852Z",
     "iopub.status.idle": "2023-08-21T03:36:08.953871Z",
     "shell.execute_reply": "2023-08-21T03:36:08.953602Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CategoricalDtype(categories=[3, 4, 5, 6, 8], ordered=False)"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto.cylinders = pd.Series(Auto.cylinders, dtype='category')\n",
    "Auto.cylinders.dtype\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adc75408",
   "metadata": {},
   "source": [
    " Now that `cylinders` is qualitative, we can display it using\n",
    " the `boxplot()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "f3d88794",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:08.955394Z",
     "iopub.status.busy": "2023-08-21T03:36:08.955297Z",
     "iopub.status.idle": "2023-08-21T03:36:09.060746Z",
     "shell.execute_reply": "2023-08-21T03:36:09.060425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32280 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "Auto.boxplot('mpg', by='cylinders', ax=ax);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62d6582f",
   "metadata": {},
   "source": [
    "The `hist()`  method can be used to plot a *histogram*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "eea49f5b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:09.062393Z",
     "iopub.status.busy": "2023-08-21T03:36:09.062272Z",
     "iopub.status.idle": "2023-08-21T03:36:09.164120Z",
     "shell.execute_reply": "2023-08-21T03:36:09.163816Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18168 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "Auto.hist('mpg', ax=ax);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5a5933c",
   "metadata": {},
   "source": [
    "The color of the bars and the number of bins can be changed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "d5bcfff8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:09.165860Z",
     "iopub.status.busy": "2023-08-21T03:36:09.165745Z",
     "iopub.status.idle": "2023-08-21T03:36:09.262378Z",
     "shell.execute_reply": "2023-08-21T03:36:09.262095Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15752 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = subplots(figsize=(8, 8))\n",
    "Auto.hist('mpg', color='red', bins=12, ax=ax);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c36b6c",
   "metadata": {},
   "source": [
    " See `Auto.hist?` for more plotting\n",
    "options.\n",
    " \n",
    "We can use the `pd.plotting.scatter_matrix()`   function to create a *scatterplot matrix* to visualize all of the pairwise relationships between the columns in\n",
    "a data frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "edb66cae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:09.264050Z",
     "iopub.status.busy": "2023-08-21T03:36:09.263936Z",
     "iopub.status.idle": "2023-08-21T03:36:10.182139Z",
     "shell.execute_reply": "2023-08-21T03:36:10.181842Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 246952 characters]",
      "text/plain": [
       "<Figure size 640x480 with 49 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.plotting.scatter_matrix(Auto);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b162bd9",
   "metadata": {},
   "source": [
    " We can also produce scatterplots\n",
    "for a subset of the variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "4f5d25d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:10.183868Z",
     "iopub.status.busy": "2023-08-21T03:36:10.183754Z",
     "iopub.status.idle": "2023-08-21T03:36:10.401318Z",
     "shell.execute_reply": "2023-08-21T03:36:10.401033Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 117244 characters]",
      "text/plain": [
       "<Figure size 640x480 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.plotting.scatter_matrix(Auto[['mpg',\n",
    "                                 'displacement',\n",
    "                                 'weight']]);\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cae5dfc",
   "metadata": {},
   "source": [
    "The `describe()`  method produces a numerical summary of each column in a data frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "ce7b23e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:10.403067Z",
     "iopub.status.busy": "2023-08-21T03:36:10.402953Z",
     "iopub.status.idle": "2023-08-21T03:36:10.408957Z",
     "shell.execute_reply": "2023-08-21T03:36:10.408683Z"
    },
    "lines_to_next_cell": 0
   },
   "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>mpg</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>392.000000</td>\n",
       "      <td>392.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>23.445918</td>\n",
       "      <td>2977.584184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>7.805007</td>\n",
       "      <td>849.402560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>9.000000</td>\n",
       "      <td>1613.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>17.000000</td>\n",
       "      <td>2225.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>22.750000</td>\n",
       "      <td>2803.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>29.000000</td>\n",
       "      <td>3614.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>46.600000</td>\n",
       "      <td>5140.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              mpg       weight\n",
       "count  392.000000   392.000000\n",
       "mean    23.445918  2977.584184\n",
       "std      7.805007   849.402560\n",
       "min      9.000000  1613.000000\n",
       "25%     17.000000  2225.250000\n",
       "50%     22.750000  2803.500000\n",
       "75%     29.000000  3614.750000\n",
       "max     46.600000  5140.000000"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto[['mpg', 'weight']].describe()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5042294",
   "metadata": {},
   "source": [
    "We can also produce a summary of just a single column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "a6545d2f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-21T03:36:10.410500Z",
     "iopub.status.busy": "2023-08-21T03:36:10.410393Z",
     "iopub.status.idle": "2023-08-21T03:36:10.414627Z",
     "shell.execute_reply": "2023-08-21T03:36:10.414350Z"
    },
    "lines_to_next_cell": 0
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    392.000000\n",
       "mean      23.445918\n",
       "std        7.805007\n",
       "min        9.000000\n",
       "25%       17.000000\n",
       "50%       22.750000\n",
       "75%       29.000000\n",
       "max       46.600000\n",
       "Name: mpg, dtype: float64"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Auto['cylinders'].describe()\n",
    "Auto['mpg'].describe()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2ea7f81",
   "metadata": {},
   "source": [
    "To exit `Jupyter`,  select `File / Close and Halt`.\n",
    "\n",
    " \n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "formats": "ipynb,Rmd",
   "main_language": "python"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
