{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4591728b0e94385d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# You must run this cell, but you can ignore its contents.\n",
    "\n",
    "import hashlib\n",
    "\n",
    "def ads_hash(ty):\n",
    "    \"\"\"Return a unique string for input\"\"\"\n",
    "    ty_str = str(ty).encode()\n",
    "    m = hashlib.sha256()\n",
    "    m.update(ty_str)\n",
    "    return m.hexdigest()[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-b2a0e9ec110c03e1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bfeafe88c3a0be38",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Let's get started by making a sample dataframe with fake data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-710e679d69e83ae2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>number</th>\n",
       "      <th>color</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>blue</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>blue</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>red</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>234</td>\n",
       "      <td>red</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>red</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3</td>\n",
       "      <td>blue</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>blue</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>red</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>green</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2</td>\n",
       "      <td>yellow</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number   color\n",
       "0       1    blue\n",
       "1       2    blue\n",
       "2       3     red\n",
       "3     234     red\n",
       "4       2     red\n",
       "5       3    blue\n",
       "6       2    blue\n",
       "7       2     red\n",
       "8       1   green\n",
       "9       2  yellow"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sample_df = pd.DataFrame({'number':[1,2,3,234,2,3,2,2,1,2], 'color':['blue','blue','red','red','red','blue','blue','red','green','yellow']})\n",
    "display(sample_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0320bf7579e7a5e0",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1 Create a Series named `condition` which is true for all rows where the color is red"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c57131439b7d2882",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "condition = sample_df[\"color\"]==\"red\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-c53eedea62c243e1",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(condition)=='28612bf01a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ac32ae211af0e86d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2 Create a new `DataFrame` named `red_sample_df` which contains only the rows with red color from `sample_df`.\n",
    "\n",
    "Hint: use your `condition` Series from above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c4be64a1d42e707d",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "red_sample_df = sample_df[ condition ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-3c7eee81fa01c6e7",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(red_sample_df)=='354bd7ec89'\n",
    "assert ads_hash(sample_df)=='21692a4d62'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-98dc0525f2068315",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Now let's use the DataFrame `.groupby()` method to find the mean value of `number` for each `color`. (Hint: this will be useful later in this exercise.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-43c0e1a1816c47a2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "blue 2.0\n",
      "green 1.0\n",
      "red 60.25\n",
      "yellow 2.0\n"
     ]
    }
   ],
   "source": [
    "for color, gdf in sample_df.groupby('color'):\n",
    "    mean_number = gdf[\"number\"].mean()\n",
    "    print(f\"{color} {mean_number}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9f757e6f5f690e81",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Using Pandas to work with real data\n",
    "\n",
    "<img src=\"data:image/png;base64,[encoded data, 116484 characters]\" alt=\"\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-927793ccea2a12d7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Get the CSV file and upload it to Jupyter\n",
    "\n",
    "* Go to https://datadryad.org/stash/dataset/doi:10.5061/dryad.vb06d3k and download the data by clicking \"Download Dataset\".\n",
    " * Check that now have a file called `doi_10.5061_dryad.vb06d3k__v1.zip` about 99KB in size in your Downloads.\n",
    " * Unzip this file, which should create:\n",
    "   - `spectra.csv`\n",
    "   - `data_onevalueperbee.csv`\n",
    "   - `data_visitationsequence.csv`\n",
    " * We are going focus on the file `data_onevalueperbee.csv`. Upload this file to your Jupyter server using the `Upload` button in the file view. Upload it into the same directory as this `.ipynb` file. Alternatively, if you are running this on your own computer, you can copy this `.csv` file into the directory with your `.ipynb` file.\n",
    "\n",
    "## Look at the file\n",
    "\n",
    "As we have covered, CSV stands for \"Comma separated values\" and is one of the most widely used file formats for scientific data. CSV files are typically like tables, potentially with column names in the first line. The lines of the file are then rows of the table. Spreadsheet programs can open most CSV files, sometimes with a few settings being required to deal with various CSV dialects.\n",
    "\n",
    "Let's take a look at this CSV file. This is mostly a repitition of what we recently looked at. We open the CSV file read the first few lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0964768c19f2b15e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'bee;nest;treatment;tstartforaging;logtstartforaging;maxminusstarttime;sumfeedingtime;logsumfeedingtime;numberofvisitations;sumdistcovered;meanvisitduration;revisitations;countyellow;countorange;countblue;visitblue;visitorange;visityellow;allcolorsvisited;averagedistance;meanspeed;sumflowersvisited;visitspatchA;visitspatchB;visitspatchC;patchesvisited;allpatchesvisited;visitsperpatchrounded\\r\\n'\n",
      "b'201531004;3;control;815;2.911157609;1965;80;1.903089987;21;1287.688282;3.80952381;4;14;4;3;1;1;1;1;80.4805176;2.767026543;12;6;13;2;3;1;7\\r\\n'\n",
      "b'201531005;3;control;108;2.033423755;;1;0;1;;1;0;1;0;0;0;0;1;0;;;1;1;0;0;1;0;0\\r\\n'\n",
      "b'201531006;3;control;90;1.954242509;788;136;2.133538908;56;2787.280939;2.428571429;9;41;10;5;1;1;1;1;60.5930639;4.572772117;21;9;25;22;3;1;19\\r\\n'\n"
     ]
    }
   ],
   "source": [
    "# Open the file in Python. The variable `fd` will be assigned the open file object.\n",
    "fd = open('data_onevalueperbee.csv', mode=\"rb\")\n",
    "\n",
    "# Iterate over the lines in the file, also creating an integer called `line_number` via the\n",
    "# use of the `enumerate()` function.\n",
    "for (line_number, line) in enumerate(fd.readlines()):\n",
    "    # Print each line.\n",
    "    print(line)\n",
    "    if line_number >= 3:\n",
    "        # Stop after a few lines\n",
    "        break\n",
    "# Close the open file object.\n",
    "fd.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5dd558e28f33dab8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "So, note that instead of a comma (`,`) separating the columns, we have a semicolon (`;`) instead. So we will need to use this when calling the Pandas `read_csv()` function. Thus, we must use the `sep=';'` keywoard argument. Also, this CSV file has some slightly unusual characters in it. From trial and error, I have learned that it must be opened with a `encoding='latin_1'` keyword argument to `read_csv()`.\n",
    "\n",
    "## Q3. Read the file `data_onevalueperbee.csv`? Into a dataframe named `df`.\n",
    "\n",
    "Hint: use the `read_csv` function not only with the filename as the required first positional argument, but also with the keyword arguments described above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-107a3973d184c577",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('data_onevalueperbee.csv', sep=';', encoding='latin_1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-66fcba98183cb481",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(df)=='15994f5b0e'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bdfe656f6d2cf933",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>bee</th>\n",
       "      <th>nest</th>\n",
       "      <th>treatment</th>\n",
       "      <th>tstartforaging</th>\n",
       "      <th>logtstartforaging</th>\n",
       "      <th>maxminusstarttime</th>\n",
       "      <th>sumfeedingtime</th>\n",
       "      <th>logsumfeedingtime</th>\n",
       "      <th>numberofvisitations</th>\n",
       "      <th>sumdistcovered</th>\n",
       "      <th>...</th>\n",
       "      <th>allcolorsvisited</th>\n",
       "      <th>averagedistance</th>\n",
       "      <th>meanspeed</th>\n",
       "      <th>sumflowersvisited</th>\n",
       "      <th>visitspatchA</th>\n",
       "      <th>visitspatchB</th>\n",
       "      <th>visitspatchC</th>\n",
       "      <th>patchesvisited</th>\n",
       "      <th>allpatchesvisited</th>\n",
       "      <th>visitsperpatchrounded</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>201531004</td>\n",
       "      <td>3</td>\n",
       "      <td>control</td>\n",
       "      <td>815</td>\n",
       "      <td>2.911158</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>80</td>\n",
       "      <td>1.903090</td>\n",
       "      <td>21</td>\n",
       "      <td>1287.688282</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>80.480518</td>\n",
       "      <td>2.767027</td>\n",
       "      <td>12</td>\n",
       "      <td>6</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>201531005</td>\n",
       "      <td>3</td>\n",
       "      <td>control</td>\n",
       "      <td>108</td>\n",
       "      <td>2.033424</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>201531006</td>\n",
       "      <td>3</td>\n",
       "      <td>control</td>\n",
       "      <td>90</td>\n",
       "      <td>1.954243</td>\n",
       "      <td>788.0</td>\n",
       "      <td>136</td>\n",
       "      <td>2.133539</td>\n",
       "      <td>56</td>\n",
       "      <td>2787.280939</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>60.593064</td>\n",
       "      <td>4.572772</td>\n",
       "      <td>21</td>\n",
       "      <td>9</td>\n",
       "      <td>25</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>201531101</td>\n",
       "      <td>3</td>\n",
       "      <td>control</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>320.0</td>\n",
       "      <td>61</td>\n",
       "      <td>1.785330</td>\n",
       "      <td>16</td>\n",
       "      <td>578.355534</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>72.294442</td>\n",
       "      <td>3.651351</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>201531102</td>\n",
       "      <td>3</td>\n",
       "      <td>control</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>656.0</td>\n",
       "      <td>258</td>\n",
       "      <td>2.411620</td>\n",
       "      <td>49</td>\n",
       "      <td>2845.622453</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>71.140561</td>\n",
       "      <td>6.307130</td>\n",
       "      <td>18</td>\n",
       "      <td>4</td>\n",
       "      <td>24</td>\n",
       "      <td>21</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         bee  nest treatment  tstartforaging  logtstartforaging  \\\n",
       "0  201531004     3   control             815           2.911158   \n",
       "1  201531005     3   control             108           2.033424   \n",
       "2  201531006     3   control              90           1.954243   \n",
       "3  201531101     3   control               1           0.000000   \n",
       "4  201531102     3   control               0           0.000000   \n",
       "\n",
       "   maxminusstarttime  sumfeedingtime  logsumfeedingtime  numberofvisitations  \\\n",
       "0             1965.0              80           1.903090                   21   \n",
       "1                NaN               1           0.000000                    1   \n",
       "2              788.0             136           2.133539                   56   \n",
       "3              320.0              61           1.785330                   16   \n",
       "4              656.0             258           2.411620                   49   \n",
       "\n",
       "   sumdistcovered  ... allcolorsvisited  averagedistance  meanspeed  \\\n",
       "0     1287.688282  ...                1        80.480518   2.767027   \n",
       "1             NaN  ...                0              NaN        NaN   \n",
       "2     2787.280939  ...                1        60.593064   4.572772   \n",
       "3      578.355534  ...                1        72.294442   3.651351   \n",
       "4     2845.622453  ...                1        71.140561   6.307130   \n",
       "\n",
       "   sumflowersvisited  visitspatchA  visitspatchB  visitspatchC  \\\n",
       "0                 12             6            13             2   \n",
       "1                  1             1             0             0   \n",
       "2                 21             9            25            22   \n",
       "3                  7             0            15             1   \n",
       "4                 18             4            24            21   \n",
       "\n",
       "   patchesvisited  allpatchesvisited  visitsperpatchrounded  \n",
       "0               3                  1                      7  \n",
       "1               1                  0                      0  \n",
       "2               3                  1                     19  \n",
       "3               2                  0                      5  \n",
       "4               3                  1                     16  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's look at the first few lines of the file with the pandas DataFrame `.head()` method:\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-85664fce05868fd1",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q4. How many rows of data are in the dataframe you read from the file `data_onevalueperbee.csv`? Put the answer in a variable `num_rows`.\n",
    "\n",
    "Hint: you can use `len(df)` to calculate the number of rows in the DataFrame `df`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e10d9bb147d32ad5",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "159\n"
     ]
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "num_rows = len(df)\n",
    "print(num_rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-227f7cff2325cb9a",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(num_rows)=='ff2ccb6ba4'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6b133b10967bef7a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q5. What are the unique values of the `nest` column?\n",
    "\n",
    "Put your answer in the variable `unique_nests`. Hint: use the `.unique()` method on the Series for the `nest` column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e0b36658308de988",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  4  6  8  9 10]\n"
     ]
    }
   ],
   "source": [
    "unique_nests = df['nest'].unique()\n",
    "print(unique_nests)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-208034c33360f85d",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(unique_nests)=='59e69ce283'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7802496c83a4d50a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q6. What are the unique values of the `treatment` column?\n",
    "\n",
    "Put your answer in the variable `unique_treatments`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7a258af7f499be42",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['control' 'imidacloprid']\n"
     ]
    }
   ],
   "source": [
    "unique_treatments = df['treatment'].unique()\n",
    "print(unique_treatments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-96c51bcd8e5a3ac3",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(unique_treatments) == '670c49c25a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's check which nests are in each treatment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2c145981d446e40d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "control\n",
      "[3 4 8]\n",
      "\n",
      "imidacloprid\n",
      "[ 6  9 10]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for treatment, gdf in df.groupby('treatment'):\n",
    "    print(treatment)\n",
    "    print(gdf['nest'].unique())\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e5dbccbf3a349a88",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q7. Now make a `seaborn` `boxplot` of the time to start foraging (`tstartforaging` in the CSV file) on the `y` axis, `nest` number on the `x` axis, and with the color (`hue`) being the `treatment`. Save the plot to a file named `Figure2a.png`.\n",
    "\n",
    "Your plot should look like: <img src=\"data:image/png;base64,[encoded data, 25256 characters]\" alt=\"\" />\n",
    "\n",
    "Check the left panel of [Figure 2a](https://royalsocietypublishing.org/doi/10.1098/rspb.2018.0506#RSPB20180506F2) from the Lämsä et al. *Proc. Roy Soc B.* 2018 paper. Do you see any similarities or differences with your plot?\n",
    "\n",
    "Hint: import seaborn and call its `boxplot()` function with `data`, `x`, `y`, `hue` keyword arguments with values `df`, `nest`, `tstartforaging`, `treatment`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-df2e372b53d245fd",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# Run this cell to import seaborn\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-208bdcdc93f0ff65",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='nest', ylabel='tstartforaging'>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25504 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.boxplot(data=df,x='nest',y='tstartforaging',hue='treatment')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c9c67275053aadd0",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q8. Now make a plot using a single line using seaborn's `boxplot` function which shows the duration of the foraging period on the y axis.\n",
    "\n",
    "This should be fairly similar to the second panel in [Figure 2a](https://royalsocietypublishing.org/doi/10.1098/rspb.2018.0506#RSPB20180506F2) from the Lämsä et al. Proc. Roy Soc B. 2018 paper.\n",
    "\n",
    "Hint: the duration of the foraging period is in the `'maxminusstarttime'` column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-d6bb242d388543aa",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='nest', ylabel='maxminusstarttime'>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25440 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Type your answer here\n",
    "sns.boxplot(data=df,x='nest',y='maxminusstarttime',hue='treatment')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-b926439e7eb1c2b5",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q9. Make a dictionary called `mean_duration_of_foraging_by_treatment`. Fill this with items whose keys are treatment strings (`'control'` and `'imidacloprid'`) and whose values are the mean of all `'maxminusstarttime'` values for rows in which the `treatment` variable is the same as the key. \n",
    "\n",
    "Hint: use `groupby` to group on the `treatment` column and use the `.mean()` method on the `maxminusstarttime` Series within each group data frame. Store each mean as the value in your dictionary for the key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-cdfad08c7777905d",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "control\n",
      "1394.4603174603174\n",
      "\n",
      "imidacloprid\n",
      "910.8152173913044\n",
      "\n",
      "{'control': 1394.4603174603174, 'imidacloprid': 910.8152173913044}\n"
     ]
    }
   ],
   "source": [
    "mean_duration_of_foraging_by_treatment = {}\n",
    "\n",
    "for treatment, gdf in df.groupby('treatment'):\n",
    "    print(treatment)\n",
    "    print(gdf['maxminusstarttime'].mean())\n",
    "    print()\n",
    "    mean_duration_of_foraging_by_treatment[treatment] = gdf['maxminusstarttime'].mean()\n",
    "    \n",
    "print(mean_duration_of_foraging_by_treatment)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-ad0c60eabc04c1b7",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash([(k,int(round(v))) for (k,v) in mean_duration_of_foraging_by_treatment.items()]) == '14fe49a61a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6718f15155ae17c2",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q10. Make a dictionary `num_unique_bees_per_nest` which has key-value pairs of nest number and the number of unique bees from that nest.\n",
    "\n",
    "Hint use `.groupby` on the `'nest'` column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0df074ae6ba331e7",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3: 8, 4: 23, 6: 37, 8: 34, 9: 33, 10: 24}\n"
     ]
    }
   ],
   "source": [
    "num_unique_bees_per_nest = {}\n",
    "for nest, gdf in df.groupby('nest'):\n",
    "    num_unique_bees_per_nest[nest] = len(gdf['bee'].unique())\n",
    "print(num_unique_bees_per_nest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-9fe3a1593835ed57",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(num_unique_bees_per_nest)=='ddc47911c0'"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Create Assignment",
  "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.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
