{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 16: Introduction to Pandas\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Pandas, conventionally imported as pd\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "Throughout your research career, you will undoubtedly need to handle data, possibly lots of data. The data comes in lots of formats, and you will spend much of your time **wrangling** the data to get it into a usable form.\n", "\n", "Pandas is the primary tool in the Python ecosystem for handling data. Its primary object, the `DataFrame` is extremely useful in wrangling data. We will explore some of that functionality here, and will put it to use in the next lesson." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The data set\n", "\n", "We will explore using Pandas with a real data set. We will use a data set published in [Beattie, et al., Perceptual impairment in face identification with poor sleep, *Royal Society Open Science*, **3**, 160321, 2016](https://doi.org/10.1098/rsos.160321). In this paper, researchers used the [Glasgow Facial Matching Test](https://doi.org/10.3758/BRM.42.1.286) (GMFT) to investigate how sleep deprivation affects a subject's ability to match faces, as well as the confidence the subject has in those matches. Briefly, the test works by having subjects look at a pair of faces. Two such pairs are shown below.\n", "\n", "
\n", " \n", "![GFMT faces](gfmt_faces.png)\n", "\n", "
\n", "\n", "The top two pictures are the same person, the bottom two pictures are different people. For each pair of faces, the subject gets as much time as he or she needs and then says whether or not they are the same person. The subject then rates his or her confidence in the choice.\n", "\n", "In this study, subjects also took surveys to determine properties about their sleep. The Sleep Condition Indicator (SCI) is a measure of insomnia disorder over the past month (scores of 16 and below indicate insomnia). The Pittsburgh Sleep Quality Index (PSQI) quantifies how well a subject sleeps in terms of interruptions, latency, etc. A higher score indicates poorer sleep. The Epworth Sleepiness Scale (ESS) assesses daytime drowsiness.\n", "\n", "The data set is stored in the file `~/git/bootcamp/data/gfmt_sleep.csv`. The contents of this file were adapted from [the Excel file posted on the public Dryad repository](https://doi.org/10.5061/dryad.r620r). (Note this: if you want other people to use and explore your data, make it publicly available.)\n", "\n", "This is a **CSV file**, where CSV stands for comma-separated value. This is a text file that is easily read into data structures in many programming languages. You should generally always store your data in such a format, not necessarily CSV, but a format that is open, has a well-defined specification, and is readable in many contexts. Excel files do not meet these criteria. Neither to `.mat` files.\n", "\n", "Let's take a look at the CSV file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "participant number,gender,age,correct hit percentage,correct reject percentage,percent correct,confidence when correct hit,confidence when incorrect hit,confidence when correct reject,confidence when incorrect reject,confidence when correct,confidence when incorrect,sci,psqi,ess\n", "8,f,39,65,80,72.5,91,90,93,83.5,93,90,9,13,2\n", "16,m,42,90,90,90,75.5,55.5,70.5,50,75,50,4,11,7\n", "18,f,31,90,95,92.5,89.5,90,86,81,89,88,10,9,3\n", "22,f,35,100,75,87.5,89.5,*,71,80,88,80,13,8,20\n", "27,f,74,60,65,62.5,68.5,49,61,49,65,49,13,9,12\n", "28,f,61,80,20,50,71,63,31,72.5,64.5,70.5,15,14,2\n", "30,m,32,90,75,82.5,67,56.5,66,65,66,64,16,9,3\n", "33,m,62,45,90,67.5,54,37,65,81.5,62,61,14,9,9\n", "34,f,33,80,100,90,70.5,76.5,64.5,*,68,76.5,14,12,10\n" ] } ], "source": [ "!head data/gfmt_sleep.csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first line contains the **headers** for each column. They are participant number, gender, age, etc. The data follow. There are two important things to note here. First, notice that the `gender` column has string data (`m` or `f`), while the rest of the data are numeric. Note also that there are some **missing data**, denoted by the `*`s in the file.\n", "\n", "Given the file I/O skills you recently learned, you could write some functions to parse this file and extract the data you want. You can imagine that this might be kind of painful. However, if the file format is nice and clean, like we more or less have here, we can use pre-built tools. Pandas has a very powerful function, `pd.read_csv()` that can read in a CSV file and store the contents in a convenient data structure called a **data frame**. In Pandas, the data type for a data frame is `DataFrame`, and we will use \"data frame\" and \"`DataFrame`\" interchangeably." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading in data\n", "\n", "Take a look at the [doc string of pd.read_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html). Holy cow! There are so many options we can specify for reading in a CSV file. You will likely find reasons to use many of these throughout your research. For this particular data set, we really only need the `na_values` kwarg. This specifies what characters signify that a data point is missing. The resulting data frame is populated with a **NaN**, or not-a-number, wherever this character is present in the file. In this case, we want `na_values='*'`. So, let's load in the data set." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('data/gfmt_sleep.csv', na_values='*')\n", "\n", "# Check the type\n", "type(df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now have the data stored in a data frame. We can look at it in the Jupyter notebook, since Jupyter will display it in a well-organized, pretty way." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
08f39658072.591.090.093.083.593.090.09132
116m42909090.075.555.570.550.075.050.04117
218f31909592.589.590.086.081.089.088.01093
322f351007587.589.5NaN71.080.088.080.013820
427f74606562.568.549.061.049.065.049.013912
................................................
9797f23708577.577.066.577.077.577.074.020810
9898f70908587.565.585.587.080.074.080.01987
9999f24708075.061.581.070.061.065.081.031215
100102f40756570.053.037.084.052.081.051.02247
101103f33854062.580.027.031.082.581.073.02457
\n", "

102 rows × 15 columns

\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "0 8 f 39 65 \n", "1 16 m 42 90 \n", "2 18 f 31 90 \n", "3 22 f 35 100 \n", "4 27 f 74 60 \n", ".. ... ... ... ... \n", "97 97 f 23 70 \n", "98 98 f 70 90 \n", "99 99 f 24 70 \n", "100 102 f 40 75 \n", "101 103 f 33 85 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "0 80 72.5 91.0 \n", "1 90 90.0 75.5 \n", "2 95 92.5 89.5 \n", "3 75 87.5 89.5 \n", "4 65 62.5 68.5 \n", ".. ... ... ... \n", "97 85 77.5 77.0 \n", "98 85 87.5 65.5 \n", "99 80 75.0 61.5 \n", "100 65 70.0 53.0 \n", "101 40 62.5 80.0 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "0 90.0 93.0 \n", "1 55.5 70.5 \n", "2 90.0 86.0 \n", "3 NaN 71.0 \n", "4 49.0 61.0 \n", ".. ... ... \n", "97 66.5 77.0 \n", "98 85.5 87.0 \n", "99 81.0 70.0 \n", "100 37.0 84.0 \n", "101 27.0 31.0 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "0 83.5 93.0 \n", "1 50.0 75.0 \n", "2 81.0 89.0 \n", "3 80.0 88.0 \n", "4 49.0 65.0 \n", ".. ... ... \n", "97 77.5 77.0 \n", "98 80.0 74.0 \n", "99 61.0 65.0 \n", "100 52.0 81.0 \n", "101 82.5 81.0 \n", "\n", " confidence when incorrect sci psqi ess \n", "0 90.0 9 13 2 \n", "1 50.0 4 11 7 \n", "2 88.0 10 9 3 \n", "3 80.0 13 8 20 \n", "4 49.0 13 9 12 \n", ".. ... ... ... ... \n", "97 74.0 20 8 10 \n", "98 80.0 19 8 7 \n", "99 81.0 31 2 15 \n", "100 51.0 22 4 7 \n", "101 73.0 24 5 7 \n", "\n", "[102 rows x 15 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a nice representation of the data, but we really do not need to display that many rows of the data frame in order to understand its structure. Instead, we can use the `head()` method of data frames to look at the first few rows." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
08f39658072.591.090.093.083.593.090.09132
116m42909090.075.555.570.550.075.050.04117
218f31909592.589.590.086.081.089.088.01093
322f351007587.589.5NaN71.080.088.080.013820
427f74606562.568.549.061.049.065.049.013912
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "0 8 f 39 65 \n", "1 16 m 42 90 \n", "2 18 f 31 90 \n", "3 22 f 35 100 \n", "4 27 f 74 60 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "0 80 72.5 91.0 \n", "1 90 90.0 75.5 \n", "2 95 92.5 89.5 \n", "3 75 87.5 89.5 \n", "4 65 62.5 68.5 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "0 90.0 93.0 \n", "1 55.5 70.5 \n", "2 90.0 86.0 \n", "3 NaN 71.0 \n", "4 49.0 61.0 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "0 83.5 93.0 \n", "1 50.0 75.0 \n", "2 81.0 89.0 \n", "3 80.0 88.0 \n", "4 49.0 65.0 \n", "\n", " confidence when incorrect sci psqi ess \n", "0 90.0 9 13 2 \n", "1 50.0 4 11 7 \n", "2 88.0 10 9 3 \n", "3 80.0 13 8 20 \n", "4 49.0 13 9 12 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is more manageable and gives us an overview of what the columns are. Note also the the missing data was populated with NaN." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing data frames\n", "\n", "The data frame is a convenient data structure for many reasons that will become clear as we start exploring. Let's start by looking at how data frames are indexed. Let's try to look at the first row." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "0", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py:3621\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 3620\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 3621\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n", "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx:136\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx:163\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:5198\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:5206\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: 0", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdf\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n", "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py:3505\u001b[0m, in \u001b[0;36mDataFrame.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3503\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mnlevels \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 3504\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_multilevel(key)\n\u001b[0;32m-> 3505\u001b[0m indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumns\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3506\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[1;32m 3507\u001b[0m indexer \u001b[38;5;241m=\u001b[39m [indexer]\n", "File \u001b[0;32m~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 3621\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine\u001b[38;5;241m.\u001b[39mget_loc(casted_key)\n\u001b[1;32m 3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m-> 3623\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 3624\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[1;32m 3625\u001b[0m \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[1;32m 3626\u001b[0m \u001b[38;5;66;03m# InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[1;32m 3627\u001b[0m \u001b[38;5;66;03m# the TypeError.\u001b[39;00m\n\u001b[1;32m 3628\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n", "\u001b[0;31mKeyError\u001b[0m: 0" ] } ], "source": [ "df[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yikes! Lots of errors. The problem is that we tried to index numerically by row. **We index DataFrames by columns.** And there is no column that has the name `0` in this data frame, though there could be. Instead, a might want to look at the column with the percentage of correct face matching tasks." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 72.5\n", "1 90.0\n", "2 92.5\n", "3 87.5\n", "4 62.5\n", " ... \n", "97 77.5\n", "98 87.5\n", "99 75.0\n", "100 70.0\n", "101 62.5\n", "Name: percent correct, Length: 102, dtype: float64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['percent correct']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gave us the numbers we were after. Notice that when it was printed, the index of the rows came along with it. If we wanted to pull out a single percentage correct, say corresponding to index `4`, we can do that." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62.5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['percent correct'][4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this is **not** the preferred way to do this. It is better to use `.loc`. This give the location in the data frame we want." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62.5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[4, 'percent correct']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that following `.loc`, we have the index by row then column, separated by a comma, in brackets. It is also important to note that **row indices need not be integers**. And you should not count on them being integers. In practice you will almost never use row indices, but rather use **Boolean indexing**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean indexing of data frames\n", "\n", "Let's say I wanted the percent correct of participant number 42. I can use Boolean indexing to specify the row. Specifically, I want the row for which `df['participant number'] == 42`. You can essentially plop this syntax directly when using `.loc`." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "54 85.0\n", "Name: percent correct, dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['participant number'] == 42, 'percent correct']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If I want to pull the whole record for that participant, I can use `:` for the column index." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
5442m291007085.075.0NaN64.543.074.043.03216
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "54 42 m 29 100 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "54 70 85.0 75.0 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "54 NaN 64.5 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "54 43.0 74.0 \n", "\n", " confidence when incorrect sci psqi ess \n", "54 43.0 32 1 6 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['participant number'] == 42, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the index, `54`, comes along for the ride, but we do not need it.\n", "\n", "Now, let's pull out all records of females under the age of 21. We can again use Boolean indexing, but we need to use an `&` operator. We did not cover this bitwise operator before, but the syntax is self-explanatory in the example below. Note that it is important that each Boolean operation you are doing is in parentheses because of the precedence of the operators involved." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
273f16708075.070.057.054.053.057.054.52313
295f189010095.076.583.080.0NaN80.083.02175
6658f16858585.055.030.050.040.052.535.029211
7972f18807577.567.551.566.057.067.053.02946
8885f18858585.093.092.091.089.091.591.025421
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "27 3 f 16 70 \n", "29 5 f 18 90 \n", "66 58 f 16 85 \n", "79 72 f 18 80 \n", "88 85 f 18 85 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "27 80 75.0 70.0 \n", "29 100 95.0 76.5 \n", "66 85 85.0 55.0 \n", "79 75 77.5 67.5 \n", "88 85 85.0 93.0 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "27 57.0 54.0 \n", "29 83.0 80.0 \n", "66 30.0 50.0 \n", "79 51.5 66.0 \n", "88 92.0 91.0 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "27 53.0 57.0 \n", "29 NaN 80.0 \n", "66 40.0 52.5 \n", "79 57.0 67.0 \n", "88 89.0 91.5 \n", "\n", " confidence when incorrect sci psqi ess \n", "27 54.5 23 1 3 \n", "29 83.0 21 7 5 \n", "66 35.0 29 2 11 \n", "79 53.0 29 4 6 \n", "88 91.0 25 4 21 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['age'] < 21) & (df['gender'] == 'f'), :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do something even more complicated, like pull out all females under 30 who got more than 85% of the face matching tasks correct. The code is clearer if we set up our Boolean indexing first, as follows." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 False\n", "3 False\n", "4 False\n", " ... \n", "97 False\n", "98 False\n", "99 False\n", "100 False\n", "101 False\n", "Length: 102, dtype: bool" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inds = (df[\"age\"] < 30) & (df[\"gender\"] == \"f\") & (df[\"percent correct\"] > 85)\n", "\n", "# Take a look\n", "inds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that `inds` is an array (actually a Pandas `Series`, essentially a `DataFrame` with one column) of `True`s and `False`s. When we index with it using `.loc`, we get back rows where `inds` is `True`." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
2293f281007587.589.5NaN67.060.080.060.01674
295f189010095.076.583.080.0NaN80.083.02175
306f28958087.5100.085.094.061.099.065.019712
3310f25100100100.090.0NaN85.0NaN90.0NaN171011
5644f21859087.566.029.070.029.067.029.026718
5848f23908587.567.047.069.040.067.040.01868
6051f24859590.097.041.074.073.083.055.52917
7567f25100100100.061.5NaN58.5NaN60.5NaN2889
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "22 93 f 28 100 \n", "29 5 f 18 90 \n", "30 6 f 28 95 \n", "33 10 f 25 100 \n", "56 44 f 21 85 \n", "58 48 f 23 90 \n", "60 51 f 24 85 \n", "75 67 f 25 100 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "22 75 87.5 89.5 \n", "29 100 95.0 76.5 \n", "30 80 87.5 100.0 \n", "33 100 100.0 90.0 \n", "56 90 87.5 66.0 \n", "58 85 87.5 67.0 \n", "60 95 90.0 97.0 \n", "75 100 100.0 61.5 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "22 NaN 67.0 \n", "29 83.0 80.0 \n", "30 85.0 94.0 \n", "33 NaN 85.0 \n", "56 29.0 70.0 \n", "58 47.0 69.0 \n", "60 41.0 74.0 \n", "75 NaN 58.5 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "22 60.0 80.0 \n", "29 NaN 80.0 \n", "30 61.0 99.0 \n", "33 NaN 90.0 \n", "56 29.0 67.0 \n", "58 40.0 67.0 \n", "60 73.0 83.0 \n", "75 NaN 60.5 \n", "\n", " confidence when incorrect sci psqi ess \n", "22 60.0 16 7 4 \n", "29 83.0 21 7 5 \n", "30 65.0 19 7 12 \n", "33 NaN 17 10 11 \n", "56 29.0 26 7 18 \n", "58 40.0 18 6 8 \n", "60 55.5 29 1 7 \n", "75 NaN 28 8 9 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[inds, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of interest in this exercise in Boolean indexing is that we never had to write a loop. To produce our indices, we could have done the following." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
2293f281007587.589.5NaN67.060.080.060.01674
295f189010095.076.583.080.0NaN80.083.02175
306f28958087.5100.085.094.061.099.065.019712
3310f25100100100.090.0NaN85.0NaN90.0NaN171011
5644f21859087.566.029.070.029.067.029.026718
5848f23908587.567.047.069.040.067.040.01868
6051f24859590.097.041.074.073.083.055.52917
7567f25100100100.061.5NaN58.5NaN60.5NaN2889
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "22 93 f 28 100 \n", "29 5 f 18 90 \n", "30 6 f 28 95 \n", "33 10 f 25 100 \n", "56 44 f 21 85 \n", "58 48 f 23 90 \n", "60 51 f 24 85 \n", "75 67 f 25 100 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "22 75 87.5 89.5 \n", "29 100 95.0 76.5 \n", "30 80 87.5 100.0 \n", "33 100 100.0 90.0 \n", "56 90 87.5 66.0 \n", "58 85 87.5 67.0 \n", "60 95 90.0 97.0 \n", "75 100 100.0 61.5 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "22 NaN 67.0 \n", "29 83.0 80.0 \n", "30 85.0 94.0 \n", "33 NaN 85.0 \n", "56 29.0 70.0 \n", "58 47.0 69.0 \n", "60 41.0 74.0 \n", "75 NaN 58.5 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "22 60.0 80.0 \n", "29 NaN 80.0 \n", "30 61.0 99.0 \n", "33 NaN 90.0 \n", "56 29.0 67.0 \n", "58 40.0 67.0 \n", "60 73.0 83.0 \n", "75 NaN 60.5 \n", "\n", " confidence when incorrect sci psqi ess \n", "22 60.0 16 7 4 \n", "29 83.0 21 7 5 \n", "30 65.0 19 7 12 \n", "33 NaN 17 10 11 \n", "56 29.0 26 7 18 \n", "58 40.0 18 6 8 \n", "60 55.5 29 1 7 \n", "75 NaN 28 8 9 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Initialize array of Boolean indices\n", "inds = [False] * len(df)\n", "\n", "# Iterate over the rows of the DataFrame to check if the row should be included\n", "for i, r in df.iterrows():\n", " if r['age'] < 30 and r['gender'] == 'f' and r['percent correct'] > 85:\n", " inds[i] = True\n", "\n", "# Make our seleciton with Boolean indexing\n", "df.loc[inds, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This feature, where the looping is done automatically on Pandas objects like data frames, is very powerful and saves us writing lots of lines of code. This example also showed how to use the `iterrows()` method of a data frame to iterate over the rows of a data frame. It is actually rare that you will need to do that, as we'll show next when computing with data frames." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculating with data frames\n", "\n", "Recall that a subject is said to suffer from insomnia if he or she has an SCI of 16 or below. We might like to add a column to the data frame that specifies whether or not the subject suffers from insomnia. We can conveniently compute with columns. This is done elementwise." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "3 True\n", "4 True\n", " ... \n", "97 False\n", "98 False\n", "99 False\n", "100 False\n", "101 False\n", "Name: sci, Length: 102, dtype: bool" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['sci'] <= 16" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tells use who is an insomniac. We can simply add this back to the data frame." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiessinsomnia
08f39658072.591.090.093.083.593.090.09132True
116m42909090.075.555.570.550.075.050.04117True
218f31909592.589.590.086.081.089.088.01093True
322f351007587.589.5NaN71.080.088.080.013820True
427f74606562.568.549.061.049.065.049.013912True
\n", "
" ], "text/plain": [ " participant number gender age correct hit percentage \\\n", "0 8 f 39 65 \n", "1 16 m 42 90 \n", "2 18 f 31 90 \n", "3 22 f 35 100 \n", "4 27 f 74 60 \n", "\n", " correct reject percentage percent correct confidence when correct hit \\\n", "0 80 72.5 91.0 \n", "1 90 90.0 75.5 \n", "2 95 92.5 89.5 \n", "3 75 87.5 89.5 \n", "4 65 62.5 68.5 \n", "\n", " confidence when incorrect hit confidence when correct reject \\\n", "0 90.0 93.0 \n", "1 55.5 70.5 \n", "2 90.0 86.0 \n", "3 NaN 71.0 \n", "4 49.0 61.0 \n", "\n", " confidence when incorrect reject confidence when correct \\\n", "0 83.5 93.0 \n", "1 50.0 75.0 \n", "2 81.0 89.0 \n", "3 80.0 88.0 \n", "4 49.0 65.0 \n", "\n", " confidence when incorrect sci psqi ess insomnia \n", "0 90.0 9 13 2 True \n", "1 50.0 4 11 7 True \n", "2 88.0 10 9 3 True \n", "3 80.0 13 8 20 True \n", "4 49.0 13 9 12 True " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add the column to the DataFrame\n", "df['insomnia'] = df['sci'] <= 16\n", "\n", "# Take a look\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A note about vectorization\n", "\n", "Notice how applying the `<=` operator to a `Series` resulted in **elementwise** application. This is called `vectorization`. It means that we do not have to write a `for` loop to do operations on the elements of a `Series` or other array-like object. Imagine if we had to do that with a `for` loop." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "insomnia = []\n", "for sci in df['sci']:\n", " insomnia.append(sci <= 16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is cumbersome. The vectorization allows for much more convenient calculation. Beyond that, the vectorized code is almost always faster when using Pandas and Numpy because the looping is done with compiled code under the hood. This can be done with many operators, including those you've already seen, like `+`, `-`, `*`, `/`, `**`, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying functions to Pandas objects\n", "\n", "Remember when we briefly saw the `np.mean()` function? We can compute with that as well. Let's compare the mean percent correct for insomniacs versus those who are not." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Insomniacs: 76.1\n", "Control: 81.46103896103897\n" ] } ], "source": [ "print('Insomniacs:', np.mean(df.loc[df['insomnia'], 'percent correct']))\n", "print('Control: ', np.mean(df.loc[~df['insomnia'], 'percent correct']))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that I used the `~` operator, which is a bit switcher. It changes all `True`s to `False`s and vice versa. In this case, it functions like a logical NOT.\n", "\n", "We will do a lot more computing with Pandas data frames in the next lessons. For our last demonstration in this lesson, we can quickly compute summary statistics about each column of a data frame using its `describe()` method." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant numberagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiess
count102.000000102.000000102.000000102.000000102.000000102.00000084.000000102.00000093.000000102.00000099.000000102.000000102.000000102.000000
mean52.04902037.92156983.08823577.20588280.14705974.99019658.56547671.13725561.22043074.64215761.97979822.2450985.2745107.294118
std30.02090914.02945015.09121017.56985412.04788114.16591619.56065314.98747917.67128313.61972515.9216707.5471283.4040074.426715
min1.00000016.00000035.00000020.00000040.00000029.5000007.00000019.00000017.00000024.00000024.5000000.0000000.0000000.000000
25%26.25000026.50000075.00000070.00000072.50000066.00000046.37500064.62500050.00000066.00000051.00000017.0000003.0000004.000000
50%52.50000036.50000090.00000080.00000083.75000075.00000056.25000071.25000061.00000075.75000061.50000023.5000005.0000007.000000
75%77.75000045.00000095.00000090.00000087.50000086.50000073.50000080.00000074.00000082.37500073.00000029.0000007.00000010.000000
max103.00000074.000000100.000000100.000000100.000000100.00000092.000000100.000000100.000000100.000000100.00000032.00000015.00000021.000000
\n", "
" ], "text/plain": [ " participant number age correct hit percentage \\\n", "count 102.000000 102.000000 102.000000 \n", "mean 52.049020 37.921569 83.088235 \n", "std 30.020909 14.029450 15.091210 \n", "min 1.000000 16.000000 35.000000 \n", "25% 26.250000 26.500000 75.000000 \n", "50% 52.500000 36.500000 90.000000 \n", "75% 77.750000 45.000000 95.000000 \n", "max 103.000000 74.000000 100.000000 \n", "\n", " correct reject percentage percent correct \\\n", "count 102.000000 102.000000 \n", "mean 77.205882 80.147059 \n", "std 17.569854 12.047881 \n", "min 20.000000 40.000000 \n", "25% 70.000000 72.500000 \n", "50% 80.000000 83.750000 \n", "75% 90.000000 87.500000 \n", "max 100.000000 100.000000 \n", "\n", " confidence when correct hit confidence when incorrect hit \\\n", "count 102.000000 84.000000 \n", "mean 74.990196 58.565476 \n", "std 14.165916 19.560653 \n", "min 29.500000 7.000000 \n", "25% 66.000000 46.375000 \n", "50% 75.000000 56.250000 \n", "75% 86.500000 73.500000 \n", "max 100.000000 92.000000 \n", "\n", " confidence when correct reject confidence when incorrect reject \\\n", "count 102.000000 93.000000 \n", "mean 71.137255 61.220430 \n", "std 14.987479 17.671283 \n", "min 19.000000 17.000000 \n", "25% 64.625000 50.000000 \n", "50% 71.250000 61.000000 \n", "75% 80.000000 74.000000 \n", "max 100.000000 100.000000 \n", "\n", " confidence when correct confidence when incorrect sci \\\n", "count 102.000000 99.000000 102.000000 \n", "mean 74.642157 61.979798 22.245098 \n", "std 13.619725 15.921670 7.547128 \n", "min 24.000000 24.500000 0.000000 \n", "25% 66.000000 51.000000 17.000000 \n", "50% 75.750000 61.500000 23.500000 \n", "75% 82.375000 73.000000 29.000000 \n", "max 100.000000 100.000000 32.000000 \n", "\n", " psqi ess \n", "count 102.000000 102.000000 \n", "mean 5.274510 7.294118 \n", "std 3.404007 4.426715 \n", "min 0.000000 0.000000 \n", "25% 3.000000 4.000000 \n", "50% 5.000000 7.000000 \n", "75% 7.000000 10.000000 \n", "max 15.000000 21.000000 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives us a data frame with summary statistics. Note that in this data frame, the row indices are not integers, but are the names of the summary statistics. If we wanted to extract the median value of each entry, we could do that with `.loc`." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "participant number 52.50\n", "age 36.50\n", "correct hit percentage 90.00\n", "correct reject percentage 80.00\n", "percent correct 83.75\n", "confidence when correct hit 75.00\n", "confidence when incorrect hit 56.25\n", "confidence when correct reject 71.25\n", "confidence when incorrect reject 61.00\n", "confidence when correct 75.75\n", "confidence when incorrect 61.50\n", "sci 23.50\n", "psqi 5.00\n", "ess 7.00\n", "Name: 50%, dtype: float64" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe().loc['50%', :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outputting a new CSV file\n", "\n", "Now that we added the insomniac column, we might like to save our data frame as a new CSV that we can reload later. We use `df.to_csv()` for this with the `index` kwarg to ask Pandas not to explicitly write the indices to the file." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "df.to_csv('gfmt_sleep_with_insomnia.csv', index=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at what this file looks like." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "participant number,gender,age,correct hit percentage,correct reject percentage,percent correct,confidence when correct hit,confidence when incorrect hit,confidence when correct reject,confidence when incorrect reject,confidence when correct,confidence when incorrect,sci,psqi,ess,insomnia\n", "8,f,39,65,80,72.5,91.0,90.0,93.0,83.5,93.0,90.0,9,13,2,True\n", "16,m,42,90,90,90.0,75.5,55.5,70.5,50.0,75.0,50.0,4,11,7,True\n", "18,f,31,90,95,92.5,89.5,90.0,86.0,81.0,89.0,88.0,10,9,3,True\n", "22,f,35,100,75,87.5,89.5,,71.0,80.0,88.0,80.0,13,8,20,True\n", "27,f,74,60,65,62.5,68.5,49.0,61.0,49.0,65.0,49.0,13,9,12,True\n", "28,f,61,80,20,50.0,71.0,63.0,31.0,72.5,64.5,70.5,15,14,2,True\n", "30,m,32,90,75,82.5,67.0,56.5,66.0,65.0,66.0,64.0,16,9,3,True\n", "33,m,62,45,90,67.5,54.0,37.0,65.0,81.5,62.0,61.0,14,9,9,True\n", "34,f,33,80,100,90.0,70.5,76.5,64.5,,68.0,76.5,14,12,10,True\n" ] } ], "source": [ "!head gfmt_sleep_with_insomnia.csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Very nice. Notice that by default Pandas leaves an empty field for NaNs, and we do not need the `na_values` kwarg when we load in this CSV file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Styling a data frame\n", "\n", "It is sometimes useful to highlight features in a data frame when viewing them. (Note that this is generally far less useful than making informative plots, which we will come to shortly.) Pandas offers some convenient ways to style the display of a data frame.\n", "\n", "As an example, let's say we wanted to highlight rows corresponding to women who scored at or above 75% correct. We can write a function that will take as an argument a row of the data frame, check the value in the `'gender'` and `'percent correct'` columns, and then specify a row color of gray or green accordingly. We then use `df.style.apply()` with the `axis=1` kwarg to apply that function to each row." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiessinsomnia
08f39658072.50000091.00000090.00000093.00000083.50000093.00000090.0000009132True
116m42909090.00000075.50000055.50000070.50000050.00000075.00000050.0000004117True
218f31909592.50000089.50000090.00000086.00000081.00000089.00000088.0000001093True
322f351007587.50000089.500000nan71.00000080.00000088.00000080.00000013820True
427f74606562.50000068.50000049.00000061.00000049.00000065.00000049.00000013912True
528f61802050.00000071.00000063.00000031.00000072.50000064.50000070.50000015142True
630m32907582.50000067.00000056.50000066.00000065.00000066.00000064.0000001693True
733m62459067.50000054.00000037.00000065.00000081.50000062.00000061.0000001499True
834f338010090.00000070.50000076.50000064.500000nan68.00000076.500000141210True
935f531005075.00000074.500000nan60.50000065.00000071.00000065.0000001487True
\n" ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def highlight_high_scoring_females(s):\n", " if s[\"gender\"] == \"f\" and s[\"percent correct\"] >= 75:\n", " return [\"background-color: #7fc97f\"] * len(s)\n", " else:\n", " return [\"background-color: lightgray\"] * len(s)\n", "\n", "df.head(10).style.apply(highlight_high_scoring_females, axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can be more fancy. Let's say we want to shade the `'percent correct'` column with a bar corresponding to the value in the column. We use the `df.style.bar()` method to do so. The `subset` kwarg specifies which columns are to have bars." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiessinsomnia
08f39658072.50000091.00000090.00000093.00000083.50000093.00000090.0000009132True
116m42909090.00000075.50000055.50000070.50000050.00000075.00000050.0000004117True
218f31909592.50000089.50000090.00000086.00000081.00000089.00000088.0000001093True
322f351007587.50000089.500000nan71.00000080.00000088.00000080.00000013820True
427f74606562.50000068.50000049.00000061.00000049.00000065.00000049.00000013912True
528f61802050.00000071.00000063.00000031.00000072.50000064.50000070.50000015142True
630m32907582.50000067.00000056.50000066.00000065.00000066.00000064.0000001693True
733m62459067.50000054.00000037.00000065.00000081.50000062.00000061.0000001499True
834f338010090.00000070.50000076.50000064.500000nan68.00000076.500000141210True
935f531005075.00000074.500000nan60.50000065.00000071.00000065.0000001487True
\n" ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10).style.bar(subset=[\"percent correct\"], vmin=0, vmax=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that I have used the `vmin=0` and `vmax=100` kwargs to set the base of the bar to be at zero and the maximum to be 100.\n", "\n", "Alternatively, I could color the percent correct according to the percent correct." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiessinsomnia
08f39658072.50000091.00000090.00000093.00000083.50000093.00000090.0000009132True
116m42909090.00000075.50000055.50000070.50000050.00000075.00000050.0000004117True
218f31909592.50000089.50000090.00000086.00000081.00000089.00000088.0000001093True
322f351007587.50000089.500000nan71.00000080.00000088.00000080.00000013820True
427f74606562.50000068.50000049.00000061.00000049.00000065.00000049.00000013912True
528f61802050.00000071.00000063.00000031.00000072.50000064.50000070.50000015142True
630m32907582.50000067.00000056.50000066.00000065.00000066.00000064.0000001693True
733m62459067.50000054.00000037.00000065.00000081.50000062.00000061.0000001499True
834f338010090.00000070.50000076.50000064.500000nan68.00000076.500000141210True
935f531005075.00000074.500000nan60.50000065.00000071.00000065.0000001487True
\n" ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10).style.background_gradient(subset=[\"percent correct\"], cmap=\"Reds\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could have multiple effects together as well." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 participant numbergenderagecorrect hit percentagecorrect reject percentagepercent correctconfidence when correct hitconfidence when incorrect hitconfidence when correct rejectconfidence when incorrect rejectconfidence when correctconfidence when incorrectscipsqiessinsomnia
08f39658072.50000091.00000090.00000093.00000083.50000093.00000090.0000009132True
116m42909090.00000075.50000055.50000070.50000050.00000075.00000050.0000004117True
218f31909592.50000089.50000090.00000086.00000081.00000089.00000088.0000001093True
322f351007587.50000089.500000nan71.00000080.00000088.00000080.00000013820True
427f74606562.50000068.50000049.00000061.00000049.00000065.00000049.00000013912True
528f61802050.00000071.00000063.00000031.00000072.50000064.50000070.50000015142True
630m32907582.50000067.00000056.50000066.00000065.00000066.00000064.0000001693True
733m62459067.50000054.00000037.00000065.00000081.50000062.00000061.0000001499True
834f338010090.00000070.50000076.50000064.500000nan68.00000076.500000141210True
935f531005075.00000074.500000nan60.50000065.00000071.00000065.0000001487True
\n" ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10).style.bar(\n", " subset=[\"percent correct\"], vmin=0, vmax=100\n", ").apply(\n", " highlight_high_scoring_females, axis=1\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In practice, I almost never use these features because it is almost always better to display results as a plot rather than in tabular form. Still, it can be useful when exploring data sets to highlight certain aspects in tabular form." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.12\n", "IPython version : 8.3.0\n", "\n", "numpy : 1.21.5\n", "pandas : 1.4.2\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,pandas,jupyterlab" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }