{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 13: 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", "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", "Let's first look at the doc string of `pd.read_csv()`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mfilepath_or_buffer\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0msep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m','\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdelimiter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mheader\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'infer'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mnames\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mindex_col\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0musecols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0msqueeze\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mprefix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmangle_dupe_cols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mengine\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mconverters\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtrue_values\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mfalse_values\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mskipinitialspace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mskiprows\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mskipfooter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mnrows\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mna_values\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkeep_default_na\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mna_filter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mskip_blank_lines\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mparse_dates\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minfer_datetime_format\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkeep_date_col\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdate_parser\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdayfirst\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0miterator\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mchunksize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcompression\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'infer'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mthousands\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34mb'.'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlineterminator\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mquotechar\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'\"'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mquoting\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdoublequote\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mescapechar\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcomment\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mencoding\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdialect\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtupleize_cols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0merror_bad_lines\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mwarn_bad_lines\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdelim_whitespace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mlow_memory\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmemory_map\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mfloat_precision\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Read a comma-separated values (csv) file into DataFrame.\n", "\n", "Also supports optionally iterating or breaking of the file\n", "into chunks.\n", "\n", "Additional help can be found in the online docs for\n", "`IO Tools `_.\n", "\n", "Parameters\n", "----------\n", "filepath_or_buffer : str, path object, or file-like object\n", " Any valid string path is acceptable. The string could be a URL. Valid\n", " URL schemes include http, ftp, s3, and file. For file URLs, a host is\n", " expected. A local file could be: file://localhost/path/to/table.csv.\n", "\n", " If you want to pass in a path object, pandas accepts either\n", " ``pathlib.Path`` or ``py._path.local.LocalPath``.\n", "\n", " By file-like object, we refer to objects with a ``read()`` method, such as\n", " a file handler (e.g. via builtin ``open`` function) or ``StringIO``.\n", "sep : str, default ','\n", " Delimiter to use. If sep is None, the C engine cannot automatically detect\n", " the separator, but the Python parsing engine can, meaning the latter will\n", " be used and automatically detect the separator by Python's builtin sniffer\n", " tool, ``csv.Sniffer``. In addition, separators longer than 1 character and\n", " different from ``'\\s+'`` will be interpreted as regular expressions and\n", " will also force the use of the Python parsing engine. Note that regex\n", " delimiters are prone to ignoring quoted data. Regex example: ``'\\r\\t'``.\n", "delimiter : str, default ``None``\n", " Alias for sep.\n", "header : int, list of int, default 'infer'\n", " Row number(s) to use as the column names, and the start of the\n", " data. Default behavior is to infer the column names: if no names\n", " are passed the behavior is identical to ``header=0`` and column\n", " names are inferred from the first line of the file, if column\n", " names are passed explicitly then the behavior is identical to\n", " ``header=None``. Explicitly pass ``header=0`` to be able to\n", " replace existing names. The header can be a list of integers that\n", " specify row locations for a multi-index on the columns\n", " e.g. [0,1,3]. Intervening rows that are not specified will be\n", " skipped (e.g. 2 in this example is skipped). Note that this\n", " parameter ignores commented lines and empty lines if\n", " ``skip_blank_lines=True``, so ``header=0`` denotes the first line of\n", " data rather than the first line of the file.\n", "names : array-like, optional\n", " List of column names to use. If file contains no header row, then you\n", " should explicitly pass ``header=None``. Duplicates in this list will cause\n", " a ``UserWarning`` to be issued.\n", "index_col : int, sequence or bool, optional\n", " Column to use as the row labels of the DataFrame. If a sequence is given, a\n", " MultiIndex is used. If you have a malformed file with delimiters at the end\n", " of each line, you might consider ``index_col=False`` to force pandas to\n", " not use the first column as the index (row names).\n", "usecols : list-like or callable, optional\n", " Return a subset of the columns. If list-like, all elements must either\n", " be positional (i.e. integer indices into the document columns) or strings\n", " that correspond to column names provided either by the user in `names` or\n", " inferred from the document header row(s). For example, a valid list-like\n", " `usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``.\n", " Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``.\n", " To instantiate a DataFrame from ``data`` with element order preserved use\n", " ``pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']]`` for columns\n", " in ``['foo', 'bar']`` order or\n", " ``pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']]``\n", " for ``['bar', 'foo']`` order.\n", "\n", " If callable, the callable function will be evaluated against the column\n", " names, returning names where the callable function evaluates to True. An\n", " example of a valid callable argument would be ``lambda x: x.upper() in\n", " ['AAA', 'BBB', 'DDD']``. Using this parameter results in much faster\n", " parsing time and lower memory usage.\n", "squeeze : bool, default False\n", " If the parsed data only contains one column then return a Series.\n", "prefix : str, optional\n", " Prefix to add to column numbers when no header, e.g. 'X' for X0, X1, ...\n", "mangle_dupe_cols : bool, default True\n", " Duplicate columns will be specified as 'X', 'X.1', ...'X.N', rather than\n", " 'X'...'X'. Passing in False will cause data to be overwritten if there\n", " are duplicate names in the columns.\n", "dtype : Type name or dict of column -> type, optional\n", " Data type for data or columns. E.g. {'a': np.float64, 'b': np.int32,\n", " 'c': 'Int64'}\n", " Use `str` or `object` together with suitable `na_values` settings\n", " to preserve and not interpret dtype.\n", " If converters are specified, they will be applied INSTEAD\n", " of dtype conversion.\n", "engine : {'c', 'python'}, optional\n", " Parser engine to use. The C engine is faster while the python engine is\n", " currently more feature-complete.\n", "converters : dict, optional\n", " Dict of functions for converting values in certain columns. Keys can either\n", " be integers or column labels.\n", "true_values : list, optional\n", " Values to consider as True.\n", "false_values : list, optional\n", " Values to consider as False.\n", "skipinitialspace : bool, default False\n", " Skip spaces after delimiter.\n", "skiprows : list-like, int or callable, optional\n", " Line numbers to skip (0-indexed) or number of lines to skip (int)\n", " at the start of the file.\n", "\n", " If callable, the callable function will be evaluated against the row\n", " indices, returning True if the row should be skipped and False otherwise.\n", " An example of a valid callable argument would be ``lambda x: x in [0, 2]``.\n", "skipfooter : int, default 0\n", " Number of lines at bottom of file to skip (Unsupported with engine='c').\n", "nrows : int, optional\n", " Number of rows of file to read. Useful for reading pieces of large files.\n", "na_values : scalar, str, list-like, or dict, optional\n", " Additional strings to recognize as NA/NaN. If dict passed, specific\n", " per-column NA values. By default the following values are interpreted as\n", " NaN: '', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan',\n", " '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'n/a', 'nan',\n", " 'null'.\n", "keep_default_na : bool, default True\n", " Whether or not to include the default NaN values when parsing the data.\n", " Depending on whether `na_values` is passed in, the behavior is as follows:\n", "\n", " * If `keep_default_na` is True, and `na_values` are specified, `na_values`\n", " is appended to the default NaN values used for parsing.\n", " * If `keep_default_na` is True, and `na_values` are not specified, only\n", " the default NaN values are used for parsing.\n", " * If `keep_default_na` is False, and `na_values` are specified, only\n", " the NaN values specified `na_values` are used for parsing.\n", " * If `keep_default_na` is False, and `na_values` are not specified, no\n", " strings will be parsed as NaN.\n", "\n", " Note that if `na_filter` is passed in as False, the `keep_default_na` and\n", " `na_values` parameters will be ignored.\n", "na_filter : bool, default True\n", " Detect missing value markers (empty strings and the value of na_values). In\n", " data without any NAs, passing na_filter=False can improve the performance\n", " of reading a large file.\n", "verbose : bool, default False\n", " Indicate number of NA values placed in non-numeric columns.\n", "skip_blank_lines : bool, default True\n", " If True, skip over blank lines rather than interpreting as NaN values.\n", "parse_dates : bool or list of int or names or list of lists or dict, default False\n", " The behavior is as follows:\n", "\n", " * boolean. If True -> try parsing the index.\n", " * list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3\n", " each as a separate date column.\n", " * list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as\n", " a single date column.\n", " * dict, e.g. {'foo' : [1, 3]} -> parse columns 1, 3 as date and call\n", " result 'foo'\n", "\n", " If a column or index cannot be represented as an array of datetimes,\n", " say because of an unparseable value or a mixture of timezones, the column\n", " or index will be returned unaltered as an object data type. For\n", " non-standard datetime parsing, use ``pd.to_datetime`` after\n", " ``pd.read_csv``. To parse an index or column with a mixture of timezones,\n", " specify ``date_parser`` to be a partially-applied\n", " :func:`pandas.to_datetime` with ``utc=True``. See\n", " :ref:`io.csv.mixed_timezones` for more.\n", "\n", " Note: A fast-path exists for iso8601-formatted dates.\n", "infer_datetime_format : bool, default False\n", " If True and `parse_dates` is enabled, pandas will attempt to infer the\n", " format of the datetime strings in the columns, and if it can be inferred,\n", " switch to a faster method of parsing them. In some cases this can increase\n", " the parsing speed by 5-10x.\n", "keep_date_col : bool, default False\n", " If True and `parse_dates` specifies combining multiple columns then\n", " keep the original columns.\n", "date_parser : function, optional\n", " Function to use for converting a sequence of string columns to an array of\n", " datetime instances. The default uses ``dateutil.parser.parser`` to do the\n", " conversion. Pandas will try to call `date_parser` in three different ways,\n", " advancing to the next if an exception occurs: 1) Pass one or more arrays\n", " (as defined by `parse_dates`) as arguments; 2) concatenate (row-wise) the\n", " string values from the columns defined by `parse_dates` into a single array\n", " and pass that; and 3) call `date_parser` once for each row using one or\n", " more strings (corresponding to the columns defined by `parse_dates`) as\n", " arguments.\n", "dayfirst : bool, default False\n", " DD/MM format dates, international and European format.\n", "iterator : bool, default False\n", " Return TextFileReader object for iteration or getting chunks with\n", " ``get_chunk()``.\n", "chunksize : int, optional\n", " Return TextFileReader object for iteration.\n", " See the `IO Tools docs\n", " `_\n", " for more information on ``iterator`` and ``chunksize``.\n", "compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'\n", " For on-the-fly decompression of on-disk data. If 'infer' and\n", " `filepath_or_buffer` is path-like, then detect compression from the\n", " following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no\n", " decompression). If using 'zip', the ZIP file must contain only one data\n", " file to be read in. Set to None for no decompression.\n", "\n", " .. versionadded:: 0.18.1 support for 'zip' and 'xz' compression.\n", "\n", "thousands : str, optional\n", " Thousands separator.\n", "decimal : str, default '.'\n", " Character to recognize as decimal point (e.g. use ',' for European data).\n", "lineterminator : str (length 1), optional\n", " Character to break file into lines. Only valid with C parser.\n", "quotechar : str (length 1), optional\n", " The character used to denote the start and end of a quoted item. Quoted\n", " items can include the delimiter and it will be ignored.\n", "quoting : int or csv.QUOTE_* instance, default 0\n", " Control field quoting behavior per ``csv.QUOTE_*`` constants. Use one of\n", " QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3).\n", "doublequote : bool, default ``True``\n", " When quotechar is specified and quoting is not ``QUOTE_NONE``, indicate\n", " whether or not to interpret two consecutive quotechar elements INSIDE a\n", " field as a single ``quotechar`` element.\n", "escapechar : str (length 1), optional\n", " One-character string used to escape other characters.\n", "comment : str, optional\n", " Indicates remainder of line should not be parsed. If found at the beginning\n", " of a line, the line will be ignored altogether. This parameter must be a\n", " single character. Like empty lines (as long as ``skip_blank_lines=True``),\n", " fully commented lines are ignored by the parameter `header` but not by\n", " `skiprows`. For example, if ``comment='#'``, parsing\n", " ``#empty\\na,b,c\\n1,2,3`` with ``header=0`` will result in 'a,b,c' being\n", " treated as the header.\n", "encoding : str, optional\n", " Encoding to use for UTF when reading/writing (ex. 'utf-8'). `List of Python\n", " standard encodings\n", " `_ .\n", "dialect : str or csv.Dialect, optional\n", " If provided, this parameter will override values (default or not) for the\n", " following parameters: `delimiter`, `doublequote`, `escapechar`,\n", " `skipinitialspace`, `quotechar`, and `quoting`. If it is necessary to\n", " override values, a ParserWarning will be issued. See csv.Dialect\n", " documentation for more details.\n", "tupleize_cols : bool, default False\n", " Leave a list of tuples on columns as is (default is to convert to\n", " a MultiIndex on the columns).\n", "\n", " .. deprecated:: 0.21.0\n", " This argument will be removed and will always convert to MultiIndex\n", "\n", "error_bad_lines : bool, default True\n", " Lines with too many fields (e.g. a csv line with too many commas) will by\n", " default cause an exception to be raised, and no DataFrame will be returned.\n", " If False, then these \"bad lines\" will dropped from the DataFrame that is\n", " returned.\n", "warn_bad_lines : bool, default True\n", " If error_bad_lines is False, and warn_bad_lines is True, a warning for each\n", " \"bad line\" will be output.\n", "delim_whitespace : bool, default False\n", " Specifies whether or not whitespace (e.g. ``' '`` or ``' '``) will be\n", " used as the sep. Equivalent to setting ``sep='\\s+'``. If this option\n", " is set to True, nothing should be passed in for the ``delimiter``\n", " parameter.\n", "\n", " .. versionadded:: 0.18.1 support for the Python parser.\n", "\n", "low_memory : bool, default True\n", " Internally process the file in chunks, resulting in lower memory use\n", " while parsing, but possibly mixed type inference. To ensure no mixed\n", " types either set False, or specify the type with the `dtype` parameter.\n", " Note that the entire file is read into a single DataFrame regardless,\n", " use the `chunksize` or `iterator` parameter to return the data in chunks.\n", " (Only valid with C parser).\n", "memory_map : bool, default False\n", " If a filepath is provided for `filepath_or_buffer`, map the file object\n", " directly onto memory and access the data directly from there. Using this\n", " option can improve performance because there is no longer any I/O overhead.\n", "float_precision : str, optional\n", " Specifies which converter the C engine should use for floating-point\n", " values. The options are `None` for the ordinary converter,\n", " `high` for the high-precision converter, and `round_trip` for the\n", " round-trip converter.\n", "\n", "Returns\n", "-------\n", "DataFrame or TextParser\n", " A comma-separated values (csv) file is returned as two-dimensional\n", " data structure with labeled axes.\n", "\n", "See Also\n", "--------\n", "to_csv : Write DataFrame to a comma-separated values (csv) file.\n", "read_csv : Read a comma-separated values (csv) file into DataFrame.\n", "read_fwf : Read a table of fixed-width formatted lines into DataFrame.\n", "\n", "Examples\n", "--------\n", ">>> pd.read_csv('data.csv') # doctest: +SKIP\n", "\u001b[0;31mFile:\u001b[0m ~/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\n", "\u001b[0;31mType:\u001b[0m function\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pd.read_csv?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 4, "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": 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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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
528f61802050.071.063.031.072.564.570.515142
630m32907582.567.056.566.065.066.064.01693
733m62459067.554.037.065.081.562.061.01499
834f338010090.070.576.564.5NaN68.076.5141210
935f531005075.074.5NaN60.565.071.065.01487
1038f41705562.582.061.573.069.082.064.014519
1141f369010095.076.575.575.0NaN76.075.51570
1246f40956580.080.089.079.058.579.563.010128
1349f24857580.058.050.049.068.055.059.014134
1455f32755565.085.081.085.086.085.083.55137
1571f404010070.069.056.070.0NaN70.056.001114
1676f611004070.069.5NaN44.573.054.573.016412
1777f42709080.087.072.090.543.588.564.0111010
1878m311007085.092.0NaN81.060.087.560.014611
1980m281005075.0100.0NaN100.0100.0100.0100.012712
2089f26608070.070.077.082.067.577.070.51481
2190m451009597.5100.0NaN100.0100.0100.0100.01496
2293f281007587.589.5NaN67.060.080.060.01674
23100f44652545.062.072.087.077.069.573.51156
24101f281004070.087.0NaN68.054.081.054.01472
251f42806572.551.544.543.049.051.049.02915
262f45809085.075.055.580.075.078.567.01951
273f16708075.070.057.054.053.057.054.52313
284f21706567.563.564.050.050.060.050.02654
295f189010095.076.583.080.0NaN80.083.02175
................................................
7264f69958087.580.065.078.570.580.070.03111
7365f311009597.598.0NaN90.040.092.040.02744
7466f44909592.587.047.569.087.083.067.03212
7567f25100100100.061.5NaN58.5NaN60.5NaN2889
7668f45705060.080.551.563.069.072.561.52541
7769f479010095.0100.0NaN71.583.097.583.03022
7870f33857077.570.038.058.565.068.040.021712
7972f18807577.567.551.566.057.067.053.02946
8073f74858082.566.055.063.050.565.055.02015
8174m21404040.090.580.074.583.082.081.02275
8275f45809587.574.067.076.017.075.064.02344
8379f37908085.095.568.083.583.094.071.02059
8481m41908587.580.059.570.041.077.059.01763
8582f41807577.594.561.586.074.092.067.02748
8683f34903562.581.052.071.058.081.058.02726
8784f39757072.557.057.059.550.058.050.022310
8885f18858585.093.092.091.089.091.591.025421
8986f311008592.5100.0NaN100.050.0100.050.03035
9087m26957585.085.088.082.082.085.085.03215
9188m66608572.567.566.074.057.074.064.03059
9291m621008090.081.0NaN74.582.079.582.03221
9392m22859590.066.056.072.063.070.559.52818
9494f41357555.055.061.080.057.072.060.031111
9595m46958087.590.075.080.080.085.075.02935
9696f56705060.063.052.567.565.564.059.52667
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", "5 28 f 61 80 \n", "6 30 m 32 90 \n", "7 33 m 62 45 \n", "8 34 f 33 80 \n", "9 35 f 53 100 \n", "10 38 f 41 70 \n", "11 41 f 36 90 \n", "12 46 f 40 95 \n", "13 49 f 24 85 \n", "14 55 f 32 75 \n", "15 71 f 40 40 \n", "16 76 f 61 100 \n", "17 77 f 42 70 \n", "18 78 m 31 100 \n", "19 80 m 28 100 \n", "20 89 f 26 60 \n", "21 90 m 45 100 \n", "22 93 f 28 100 \n", "23 100 f 44 65 \n", "24 101 f 28 100 \n", "25 1 f 42 80 \n", "26 2 f 45 80 \n", "27 3 f 16 70 \n", "28 4 f 21 70 \n", "29 5 f 18 90 \n", ".. ... ... ... ... \n", "72 64 f 69 95 \n", "73 65 f 31 100 \n", "74 66 f 44 90 \n", "75 67 f 25 100 \n", "76 68 f 45 70 \n", "77 69 f 47 90 \n", "78 70 f 33 85 \n", "79 72 f 18 80 \n", "80 73 f 74 85 \n", "81 74 m 21 40 \n", "82 75 f 45 80 \n", "83 79 f 37 90 \n", "84 81 m 41 90 \n", "85 82 f 41 80 \n", "86 83 f 34 90 \n", "87 84 f 39 75 \n", "88 85 f 18 85 \n", "89 86 f 31 100 \n", "90 87 m 26 95 \n", "91 88 m 66 60 \n", "92 91 m 62 100 \n", "93 92 m 22 85 \n", "94 94 f 41 35 \n", "95 95 m 46 95 \n", "96 96 f 56 70 \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", "5 20 50.0 71.0 \n", "6 75 82.5 67.0 \n", "7 90 67.5 54.0 \n", "8 100 90.0 70.5 \n", "9 50 75.0 74.5 \n", "10 55 62.5 82.0 \n", "11 100 95.0 76.5 \n", "12 65 80.0 80.0 \n", "13 75 80.0 58.0 \n", "14 55 65.0 85.0 \n", "15 100 70.0 69.0 \n", "16 40 70.0 69.5 \n", "17 90 80.0 87.0 \n", "18 70 85.0 92.0 \n", "19 50 75.0 100.0 \n", "20 80 70.0 70.0 \n", "21 95 97.5 100.0 \n", "22 75 87.5 89.5 \n", "23 25 45.0 62.0 \n", "24 40 70.0 87.0 \n", "25 65 72.5 51.5 \n", "26 90 85.0 75.0 \n", "27 80 75.0 70.0 \n", "28 65 67.5 63.5 \n", "29 100 95.0 76.5 \n", ".. ... ... ... \n", "72 80 87.5 80.0 \n", "73 95 97.5 98.0 \n", "74 95 92.5 87.0 \n", "75 100 100.0 61.5 \n", "76 50 60.0 80.5 \n", "77 100 95.0 100.0 \n", "78 70 77.5 70.0 \n", "79 75 77.5 67.5 \n", "80 80 82.5 66.0 \n", "81 40 40.0 90.5 \n", "82 95 87.5 74.0 \n", "83 80 85.0 95.5 \n", "84 85 87.5 80.0 \n", "85 75 77.5 94.5 \n", "86 35 62.5 81.0 \n", "87 70 72.5 57.0 \n", "88 85 85.0 93.0 \n", "89 85 92.5 100.0 \n", "90 75 85.0 85.0 \n", "91 85 72.5 67.5 \n", "92 80 90.0 81.0 \n", "93 95 90.0 66.0 \n", "94 75 55.0 55.0 \n", "95 80 87.5 90.0 \n", "96 50 60.0 63.0 \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", "5 63.0 31.0 \n", "6 56.5 66.0 \n", "7 37.0 65.0 \n", "8 76.5 64.5 \n", "9 NaN 60.5 \n", "10 61.5 73.0 \n", "11 75.5 75.0 \n", "12 89.0 79.0 \n", "13 50.0 49.0 \n", "14 81.0 85.0 \n", "15 56.0 70.0 \n", "16 NaN 44.5 \n", "17 72.0 90.5 \n", "18 NaN 81.0 \n", "19 NaN 100.0 \n", "20 77.0 82.0 \n", "21 NaN 100.0 \n", "22 NaN 67.0 \n", "23 72.0 87.0 \n", "24 NaN 68.0 \n", "25 44.5 43.0 \n", "26 55.5 80.0 \n", "27 57.0 54.0 \n", "28 64.0 50.0 \n", "29 83.0 80.0 \n", ".. ... ... \n", "72 65.0 78.5 \n", "73 NaN 90.0 \n", "74 47.5 69.0 \n", "75 NaN 58.5 \n", "76 51.5 63.0 \n", "77 NaN 71.5 \n", "78 38.0 58.5 \n", "79 51.5 66.0 \n", "80 55.0 63.0 \n", "81 80.0 74.5 \n", "82 67.0 76.0 \n", "83 68.0 83.5 \n", "84 59.5 70.0 \n", "85 61.5 86.0 \n", "86 52.0 71.0 \n", "87 57.0 59.5 \n", "88 92.0 91.0 \n", "89 NaN 100.0 \n", "90 88.0 82.0 \n", "91 66.0 74.0 \n", "92 NaN 74.5 \n", "93 56.0 72.0 \n", "94 61.0 80.0 \n", "95 75.0 80.0 \n", "96 52.5 67.5 \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", "5 72.5 64.5 \n", "6 65.0 66.0 \n", "7 81.5 62.0 \n", "8 NaN 68.0 \n", "9 65.0 71.0 \n", "10 69.0 82.0 \n", "11 NaN 76.0 \n", "12 58.5 79.5 \n", "13 68.0 55.0 \n", "14 86.0 85.0 \n", "15 NaN 70.0 \n", "16 73.0 54.5 \n", "17 43.5 88.5 \n", "18 60.0 87.5 \n", "19 100.0 100.0 \n", "20 67.5 77.0 \n", "21 100.0 100.0 \n", "22 60.0 80.0 \n", "23 77.0 69.5 \n", "24 54.0 81.0 \n", "25 49.0 51.0 \n", "26 75.0 78.5 \n", "27 53.0 57.0 \n", "28 50.0 60.0 \n", "29 NaN 80.0 \n", ".. ... ... \n", "72 70.5 80.0 \n", "73 40.0 92.0 \n", "74 87.0 83.0 \n", "75 NaN 60.5 \n", "76 69.0 72.5 \n", "77 83.0 97.5 \n", "78 65.0 68.0 \n", "79 57.0 67.0 \n", "80 50.5 65.0 \n", "81 83.0 82.0 \n", "82 17.0 75.0 \n", "83 83.0 94.0 \n", "84 41.0 77.0 \n", "85 74.0 92.0 \n", "86 58.0 81.0 \n", "87 50.0 58.0 \n", "88 89.0 91.5 \n", "89 50.0 100.0 \n", "90 82.0 85.0 \n", "91 57.0 74.0 \n", "92 82.0 79.5 \n", "93 63.0 70.5 \n", "94 57.0 72.0 \n", "95 80.0 85.0 \n", "96 65.5 64.0 \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", "5 70.5 15 14 2 \n", "6 64.0 16 9 3 \n", "7 61.0 14 9 9 \n", "8 76.5 14 12 10 \n", "9 65.0 14 8 7 \n", "10 64.0 14 5 19 \n", "11 75.5 15 7 0 \n", "12 63.0 10 12 8 \n", "13 59.0 14 13 4 \n", "14 83.5 5 13 7 \n", "15 56.0 0 11 14 \n", "16 73.0 16 4 12 \n", "17 64.0 11 10 10 \n", "18 60.0 14 6 11 \n", "19 100.0 12 7 12 \n", "20 70.5 14 8 1 \n", "21 100.0 14 9 6 \n", "22 60.0 16 7 4 \n", "23 73.5 1 15 6 \n", "24 54.0 14 7 2 \n", "25 49.0 29 1 5 \n", "26 67.0 19 5 1 \n", "27 54.5 23 1 3 \n", "28 50.0 26 5 4 \n", "29 83.0 21 7 5 \n", ".. ... ... ... ... \n", "72 70.0 31 1 1 \n", "73 40.0 27 4 4 \n", "74 67.0 32 1 2 \n", "75 NaN 28 8 9 \n", "76 61.5 25 4 1 \n", "77 83.0 30 2 2 \n", "78 40.0 21 7 12 \n", "79 53.0 29 4 6 \n", "80 55.0 20 1 5 \n", "81 81.0 22 7 5 \n", "82 64.0 23 4 4 \n", "83 71.0 20 5 9 \n", "84 59.0 17 6 3 \n", "85 67.0 27 4 8 \n", "86 58.0 27 2 6 \n", "87 50.0 22 3 10 \n", "88 91.0 25 4 21 \n", "89 50.0 30 3 5 \n", "90 85.0 32 1 5 \n", "91 64.0 30 5 9 \n", "92 82.0 32 2 1 \n", "93 59.5 28 1 8 \n", "94 60.0 31 1 11 \n", "95 75.0 29 3 5 \n", "96 59.5 26 6 7 \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": 5, "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 much. Instead, we can use the `head()` method of data frames to look at the first few rows." ] }, { "cell_type": "code", "execution_count": 6, "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": 6, "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": 7, "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)", "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 2656\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2657\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2658\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: 0", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 2925\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2926\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2927\u001b[0;31m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2928\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2929\u001b[0m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 2657\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2658\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2659\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2660\u001b[0m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2661\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\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": 8, "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", "5 50.0\n", "6 82.5\n", "7 67.5\n", "8 90.0\n", "9 75.0\n", "10 62.5\n", "11 95.0\n", "12 80.0\n", "13 80.0\n", "14 65.0\n", "15 70.0\n", "16 70.0\n", "17 80.0\n", "18 85.0\n", "19 75.0\n", "20 70.0\n", "21 97.5\n", "22 87.5\n", "23 45.0\n", "24 70.0\n", "25 72.5\n", "26 85.0\n", "27 75.0\n", "28 67.5\n", "29 95.0\n", " ... \n", "72 87.5\n", "73 97.5\n", "74 92.5\n", "75 100.0\n", "76 60.0\n", "77 95.0\n", "78 77.5\n", "79 77.5\n", "80 82.5\n", "81 40.0\n", "82 87.5\n", "83 85.0\n", "84 87.5\n", "85 77.5\n", "86 62.5\n", "87 72.5\n", "88 85.0\n", "89 92.5\n", "90 85.0\n", "91 72.5\n", "92 90.0\n", "93 90.0\n", "94 55.0\n", "95 87.5\n", "96 60.0\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": 8, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62.5" ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62.5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[4, 'percent correct']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "54 85.0\n", "Name: percent correct, dtype: float64" ] }, "execution_count": 11, "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": 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", "
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": 12, "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": 13, "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": 13, "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": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 False\n", "3 False\n", "4 False\n", "5 False\n", "6 False\n", "7 False\n", "8 False\n", "9 False\n", "10 False\n", "11 False\n", "12 False\n", "13 False\n", "14 False\n", "15 False\n", "16 False\n", "17 False\n", "18 False\n", "19 False\n", "20 False\n", "21 False\n", "22 True\n", "23 False\n", "24 False\n", "25 False\n", "26 False\n", "27 False\n", "28 False\n", "29 True\n", " ... \n", "72 False\n", "73 False\n", "74 False\n", "75 True\n", "76 False\n", "77 False\n", "78 False\n", "79 False\n", "80 False\n", "81 False\n", "82 False\n", "83 False\n", "84 False\n", "85 False\n", "86 False\n", "87 False\n", "88 False\n", "89 False\n", "90 False\n", "91 False\n", "92 False\n", "93 False\n", "94 False\n", "95 False\n", "96 False\n", "97 False\n", "98 False\n", "99 False\n", "100 False\n", "101 False\n", "Length: 102, dtype: bool" ] }, "execution_count": 14, "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": 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": [ "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": 16, "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": 16, "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": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "3 True\n", "4 True\n", "5 True\n", "6 True\n", "7 True\n", "8 True\n", "9 True\n", "10 True\n", "11 True\n", "12 True\n", "13 True\n", "14 True\n", "15 True\n", "16 True\n", "17 True\n", "18 True\n", "19 True\n", "20 True\n", "21 True\n", "22 True\n", "23 True\n", "24 True\n", "25 False\n", "26 False\n", "27 False\n", "28 False\n", "29 False\n", " ... \n", "72 False\n", "73 False\n", "74 False\n", "75 False\n", "76 False\n", "77 False\n", "78 False\n", "79 False\n", "80 False\n", "81 False\n", "82 False\n", "83 False\n", "84 False\n", "85 False\n", "86 False\n", "87 False\n", "88 False\n", "89 False\n", "90 False\n", "91 False\n", "92 False\n", "93 False\n", "94 False\n", "95 False\n", "96 False\n", "97 False\n", "98 False\n", "99 False\n", "100 False\n", "101 False\n", "Name: sci, Length: 102, dtype: bool" ] }, "execution_count": 17, "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": 18, "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": 18, "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": 19, "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": 20, "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 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": 21, "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": 21, "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": 22, "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": 22, "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": 23, "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": 24, "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": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.7.7\n", "IPython 7.15.0\n", "\n", "numpy 1.18.1\n", "pandas 0.24.2\n", "jupyterlab 2.1.4\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,pandas,jupyterlab" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }