ASPP 2024 material

This commit is contained in:
Pietro Berkes 2024-08-27 15:27:53 +03:00
commit 1f6bc07c51
90 changed files with 91689 additions and 0 deletions

View file

@ -0,0 +1,316 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8cc1c960",
"metadata": {},
"source": [
"# Pandas, quick introduction"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0f55dab1",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "4b377c42",
"metadata": {},
"source": [
"# Pandas introduces a tabular data structure, the DataFrame\n",
"\n",
"* Columns can be of any C-native type\n",
"* Columns and rows have indices, i.e. labels that identify each column or row"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ec75edbe",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(\n",
" data = [\n",
" ['Anthony', 28, 1.53], \n",
" ['Maria', 31, 1.76], \n",
" ['Emma', 26, 1.83], \n",
" ['Philip', 41, 1.81], \n",
" ['Bill', 27, None],\n",
" ],\n",
" columns = ['name', 'age', 'height'],\n",
" index=['A484', 'C012', 'A123', 'B663', 'A377'],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37318480",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe1c5739",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "dedad6f3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "e31f21c6",
"metadata": {},
"source": [
"## DataFrame attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4109f1eb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "708f9bb5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cb2f33b9",
"metadata": {},
"source": [
"## Indexing rows and columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19ef2738",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f354ffc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "94563f03",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "43ab5233",
"metadata": {},
"source": [
"## Examining a column"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2cb544c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "86388f86",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "fc081b90",
"metadata": {},
"source": [
"# Filtering"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "263ae06c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "318da062",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a570023a",
"metadata": {},
"source": [
"# Basic operations are by column (unlike NumPy)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7260d212",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "49b7057a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5a0f053",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e1ffe32",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7cf9b5d7",
"metadata": {},
"source": [
"# Operations on strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b78bc237",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0236069f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5761725b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce3d54ad",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "8c5584db",
"metadata": {},
"source": [
"# Adding new columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6e09176",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9a552f0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e354ace",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,462 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "37957eb0",
"metadata": {},
"source": [
"# Combine information across tables: joins and anti-joins"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b6f949f7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "6a7fcf90",
"metadata": {},
"source": [
"# \"Load\" some experimental data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a9450803",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>711</td>\n",
" <td>A2</td>\n",
" <td>0.44</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 711 A1 4.01 RIGHT\n",
"4 711 A2 0.44 LEFT\n",
"5 313 A1 0.07 RIGHT\n",
"6 313 B1 0.08 RIGHT\n",
"7 712 A2 3.29 LEFT\n",
"8 314 A2 0.29 LEFT\n",
"9 714 B2 3.32 RIGHT\n",
"10 314 B1 0.14 RIGHT\n",
"11 314 C2 0.73 RIGHT\n",
"12 713 B1 5.74 LEFT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['711', 'A2', 0.44, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "9f6de0d6",
"metadata": {},
"source": [
"Each experiment belongs to one experimental condition, but the parameters of each condition are not in the table"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "455471d7",
"metadata": {},
"outputs": [],
"source": [
"condition_to_orientation = {\n",
" 'A1': 0,\n",
" 'A2': 0,\n",
" 'B1': 45,\n",
" 'B2': 45,\n",
" 'C1': 90,\n",
"}\n",
"\n",
"condition_to_duration = {\n",
" 'A1': 0.1,\n",
" 'A2': 0.01,\n",
" 'B1': 0.1,\n",
" 'B2': 0.01,\n",
" 'C1': 0.2,\n",
"}\n",
"\n",
"condition_to_surround = {\n",
" 'A1': 'FULL',\n",
" 'A2': 'NONE',\n",
" 'B1': 'NONE',\n",
" 'B2': 'FULL',\n",
" 'C1': 'FULL',\n",
"}\n",
"\n",
"\n",
"condition_to_stimulus_type = {\n",
" 'A1': 'LINES',\n",
" 'A2': 'DOTS',\n",
" 'B1': 'PLAID',\n",
" 'B2': 'PLAID',\n",
" 'C1': 'WIGGLES',\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "5ccfd7e7",
"metadata": {},
"source": [
"# Manually adding the condition parameters to the table"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "cc32110c",
"metadata": {},
"outputs": [],
"source": [
"data_with_properties = data.copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06263dc6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b96962b2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "d6e71b13",
"metadata": {},
"source": [
"# Using a join operation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d9835d7c",
"metadata": {},
"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>orientation</th>\n",
" <th>duration</th>\n",
" <th>surround</th>\n",
" <th>stimulus_type</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0</td>\n",
" <td>0.1</td>\n",
" <td>FULL</td>\n",
" <td>LINES</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>0</td>\n",
" <td>0.01</td>\n",
" <td>NONE</td>\n",
" <td>DOTS</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>45</td>\n",
" <td>0.1</td>\n",
" <td>NONE</td>\n",
" <td>PLAID</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>45</td>\n",
" <td>0.01</td>\n",
" <td>FULL</td>\n",
" <td>PLAID</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C1</th>\n",
" <td>90</td>\n",
" <td>0.2</td>\n",
" <td>FULL</td>\n",
" <td>WIGGLES</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" orientation duration surround stimulus_type\n",
"A1 0 0.1 FULL LINES\n",
"A2 0 0.01 NONE DOTS\n",
"B1 45 0.1 NONE PLAID\n",
"B2 45 0.01 FULL PLAID\n",
"C1 90 0.2 FULL WIGGLES"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Often, this is done using a spreadsheet\n",
"condition_properties = pd.DataFrame(\n",
" [condition_to_orientation, condition_to_duration, condition_to_surround, condition_to_stimulus_type],\n",
" index=['orientation', 'duration', 'surround', 'stimulus_type'],\n",
").T\n",
"condition_properties"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c27ea9f3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e563cd0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cba9534f",
"metadata": {},
"source": [
"# Anti-join: filter out unwanted data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1cb2bbdb",
"metadata": {},
"outputs": [],
"source": [
"# We are given a list of subjects that are outliers and should be disregarded in the analysis\n",
"outliers = pd.DataFrame([['711'], ['712'], ['713'], ['714'], ['888']], columns=['subject_id'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0e2c3c5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "90d92640",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,814 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Split-apply-combine operations for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba193f3f",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 313 A1 0.07 RIGHT\n",
"4 313 B1 0.08 RIGHT\n",
"5 314 A2 0.29 LEFT\n",
"6 314 B1 0.14 RIGHT\n",
"7 314 C2 0.73 RIGHT\n",
"8 711 A1 4.01 RIGHT\n",
"9 712 A2 3.29 LEFT\n",
"10 713 B1 5.74 LEFT\n",
"11 714 B2 3.32 RIGHT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "8a239e0c",
"metadata": {},
"source": [
"# Group-by"
]
},
{
"cell_type": "markdown",
"id": "31eba91e",
"metadata": {},
"source": [
"We want to compute the mean response time by condition.\n",
"\n",
"Let's start by doing it by hand, using for loops!"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e8331039",
"metadata": {},
"outputs": [],
"source": [
"conditions = data['condition_id'].unique()\n",
"results_dict = {}\n",
"for condition in conditions:\n",
" group = data[data['condition_id'] == condition]\n",
" results_dict[condition] = group['response_time'].mean()\n",
"\n",
"results = pd.DataFrame([results_dict], index=['response_time']).T"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "09cb04c4",
"metadata": {},
"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>response_time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>1.400000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.705000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>1.986667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>3.320000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" response_time\n",
"A1 1.400000\n",
"A2 1.316667\n",
"C2 0.705000\n",
"B1 1.986667\n",
"B2 3.320000"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results"
]
},
{
"cell_type": "markdown",
"id": "2bc09c66",
"metadata": {},
"source": [
"This is a basic operation, and we would need to repeat his pattern a million times!\n",
"\n",
"Pandas and all other tools for tabular data provide a command for performing operations on groups."
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "0500cd4a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pandas.core.groupby.generic.DataFrameGroupBy object at 0x14ff67a90>"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# df.groupby(column_name) groups a DataFrame by the values in the column\n",
"data.groupby('condition_id')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c5857c4e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 3\n",
"A2 3\n",
"B1 3\n",
"B2 1\n",
"C2 2\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The group-by object can by used as a DataFrame. \n",
"# Operations are executed on each group individually, then aggregated\n",
"data.groupby('condition_id').size()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "5c865cc1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 1.400000\n",
"A2 1.316667\n",
"B1 1.986667\n",
"B2 3.320000\n",
"C2 0.705000\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.groupby('condition_id')['response_time'].mean()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "615a4515",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 4.01\n",
"A2 3.29\n",
"B1 5.74\n",
"B2 3.32\n",
"C2 0.73\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.groupby('condition_id')['response_time'].max()"
]
},
{
"cell_type": "markdown",
"id": "b0441458",
"metadata": {},
"source": [
"# Pivot tables"
]
},
{
"cell_type": "markdown",
"id": "3feec98d",
"metadata": {},
"source": [
"We want to look at response time biases when the subjects respond LEFT vs RIGHT. In principle, we expect them to have the same response time in both cases.\n",
"\n",
"We compute a summary table with 1) condition_id on the rows; 2) response on the columns; 3) the average response time for all experiments with a that condition and response\n",
"\n",
"We can do it with `groupby`, with some table manipulation commands."
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "4a8a7d0d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id response\n",
"A1 LEFT 0.120000\n",
" RIGHT 2.040000\n",
"A2 LEFT 1.316667\n",
"B1 LEFT 5.740000\n",
" RIGHT 0.110000\n",
"B2 RIGHT 3.320000\n",
"C2 LEFT 0.680000\n",
" RIGHT 0.730000\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary = data.groupby(['condition_id', 'response'])['response_time'].mean()\n",
"summary"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "e5a645e0",
"metadata": {},
"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>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"response LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04\n",
"A2 1.316667 NaN\n",
"B1 5.740000 0.11\n",
"B2 NaN 3.32\n",
"C2 0.680000 0.73"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary.unstack(level=1)"
]
},
{
"cell_type": "markdown",
"id": "3307fcc6",
"metadata": {},
"source": [
"Pandas has a command called `pivot_table` that can be used to perform this kind of operation straightforwardly."
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "8941edfe",
"metadata": {},
"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>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"response LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04\n",
"A2 1.316667 NaN\n",
"B1 5.740000 0.11\n",
"B2 NaN 3.32\n",
"C2 0.680000 0.73"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.pivot_table(index='condition_id', columns='response', values='response_time', aggfunc='mean')"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "a7d1d998",
"metadata": {},
"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 tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">mean</th>\n",
" <th colspan=\"2\" halign=\"left\">std</th>\n",
" <th colspan=\"2\" halign=\"left\">count</th>\n",
" </tr>\n",
" <tr>\n",
" <th>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" <td>NaN</td>\n",
" <td>2.786001</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" <td>1.709425</td>\n",
" <td>NaN</td>\n",
" <td>3.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" <td>NaN</td>\n",
" <td>0.042426</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" mean std count \n",
"response LEFT RIGHT LEFT RIGHT LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04 NaN 2.786001 1.0 2.0\n",
"A2 1.316667 NaN 1.709425 NaN 3.0 NaN\n",
"B1 5.740000 0.11 NaN 0.042426 1.0 2.0\n",
"B2 NaN 3.32 NaN NaN NaN 1.0\n",
"C2 0.680000 0.73 NaN NaN 1.0 1.0"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
" data\n",
" .pivot_table(\n",
" index='condition_id', \n",
" columns='response', \n",
" values='response_time', \n",
" aggfunc=['mean', 'std', 'count'],\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a770b812",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c77c2dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,335 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Split-apply-combine operations for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba193f3f",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 313 A1 0.07 RIGHT\n",
"4 313 B1 0.08 RIGHT\n",
"5 314 A2 0.29 LEFT\n",
"6 314 B1 0.14 RIGHT\n",
"7 314 C2 0.73 RIGHT\n",
"8 711 A1 4.01 RIGHT\n",
"9 712 A2 3.29 LEFT\n",
"10 713 B1 5.74 LEFT\n",
"11 714 B2 3.32 RIGHT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "8a239e0c",
"metadata": {},
"source": [
"# Group-by"
]
},
{
"cell_type": "markdown",
"id": "31eba91e",
"metadata": {},
"source": [
"We want to compute the mean response time by condition.\n",
"\n",
"Let's start by doing it by hand, using for loops!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff3f890b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "805d04c7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "2bc09c66",
"metadata": {},
"source": [
"This is a basic operation, and we would need to repeat his pattern a million times!\n",
"\n",
"Pandas and all other tools for tabular data provide a command for performing operations on groups."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcc8c9c7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "818b8346",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b0441458",
"metadata": {},
"source": [
"# Pivot tables"
]
},
{
"cell_type": "markdown",
"id": "3feec98d",
"metadata": {},
"source": [
"We want to look at response time biases when the subjects respond LEFT vs RIGHT. In principle, we expect them to have the same response time in both cases.\n",
"\n",
"We compute a summary table with 1) condition_id on the rows; 2) response on the columns; 3) the average response time for all experiments with a that condition and response\n",
"\n",
"We can do it with `groupby`, with some table manipulation commands."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04f6ff60",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "62600721",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "3307fcc6",
"metadata": {},
"source": [
"Pandas has a command called `pivot_table` that can be used to perform this kind of operation straightforwardly."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a770b812",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c77c2dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,320 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Window functions for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "83bbd275",
"metadata": {},
"source": [
"# Load experimental data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "88b9e189",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('timed_responses.csv', index_col=0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "987a3518",
"metadata": {},
"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>subject_id</th>\n",
" <th>time (ms)</th>\n",
" <th>response</th>\n",
" <th>accuracy</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>574</th>\n",
" <td>3</td>\n",
" <td>540</td>\n",
" <td>RIGHT</td>\n",
" <td>0.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1190</th>\n",
" <td>2</td>\n",
" <td>552</td>\n",
" <td>LEFT</td>\n",
" <td>0.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1895</th>\n",
" <td>2</td>\n",
" <td>1036</td>\n",
" <td>LEFT</td>\n",
" <td>0.36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>53</th>\n",
" <td>3</td>\n",
" <td>257</td>\n",
" <td>RIGHT</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>158</th>\n",
" <td>2</td>\n",
" <td>743</td>\n",
" <td>RIGHT</td>\n",
" <td>0.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>551</th>\n",
" <td>3</td>\n",
" <td>619</td>\n",
" <td>LEFT</td>\n",
" <td>0.25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1602</th>\n",
" <td>1</td>\n",
" <td>43</td>\n",
" <td>RIGHT</td>\n",
" <td>0.65</td>\n",
" </tr>\n",
" <tr>\n",
" <th>413</th>\n",
" <td>1</td>\n",
" <td>471</td>\n",
" <td>LEFT</td>\n",
" <td>0.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>785</th>\n",
" <td>1</td>\n",
" <td>121</td>\n",
" <td>LEFT</td>\n",
" <td>0.10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1393</th>\n",
" <td>2</td>\n",
" <td>903</td>\n",
" <td>RIGHT</td>\n",
" <td>0.33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>629</th>\n",
" <td>2</td>\n",
" <td>353</td>\n",
" <td>LEFT</td>\n",
" <td>0.17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1829</th>\n",
" <td>3</td>\n",
" <td>768</td>\n",
" <td>RIGHT</td>\n",
" <td>0.26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>902</th>\n",
" <td>1</td>\n",
" <td>1093</td>\n",
" <td>LEFT</td>\n",
" <td>0.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1486</th>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>RIGHT</td>\n",
" <td>0.29</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id time (ms) response accuracy\n",
"574 3 540 RIGHT 0.04\n",
"1190 2 552 LEFT 0.43\n",
"1895 2 1036 LEFT 0.36\n",
"53 3 257 RIGHT 0.11\n",
"158 2 743 RIGHT 0.32\n",
"551 3 619 LEFT 0.25\n",
"1602 1 43 RIGHT 0.65\n",
"413 1 471 LEFT 0.80\n",
"785 1 121 LEFT 0.10\n",
"1393 2 903 RIGHT 0.33\n",
"629 2 353 LEFT 0.17\n",
"1829 3 768 RIGHT 0.26\n",
"902 1 1093 LEFT 0.34\n",
"1486 2 3 RIGHT 0.29"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"id": "5c41cd93",
"metadata": {},
"source": [
"# Split-apply-combine operations return one aggregated value per group"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": [
"df.groupby('subject_id')['accuracy'].max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b2a1796",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "2bb99152",
"metadata": {},
"source": [
"# However, for some calculations we need to have a value per row\n",
"\n",
"For example: for each subject, rank the responses by decreasing accuracy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3aed0755",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "17f3d40f",
"metadata": {},
"source": [
"# In many cases, a window functions is combined with a sorting operation\n",
"\n",
"For example: for each subject, count the number of \"LEFT\" responses up until any moment in the experiment"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67efdd56",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a00b4f39",
"metadata": {},
"source": [
"# Window functions are also useful to compute changes in the data for each group\n",
"\n",
"In this case, the window function often uses the `shift(n)` method that lags the data by `n` rows"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e553c17f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2973e3d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ca46b0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,316 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8cc1c960",
"metadata": {},
"source": [
"# Pandas, quick introduction"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0f55dab1",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "4b377c42",
"metadata": {},
"source": [
"# Pandas introduces a tabular data structure, the DataFrame\n",
"\n",
"* Columns can be of any C-native type\n",
"* Columns and rows have indices, i.e. labels that identify each column or row"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ec75edbe",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(\n",
" data = [\n",
" ['Anthony', 28, 1.53], \n",
" ['Maria', 31, 1.76], \n",
" ['Emma', 26, 1.83], \n",
" ['Philip', 41, 1.81], \n",
" ['Bill', 27, None],\n",
" ],\n",
" columns = ['name', 'age', 'height'],\n",
" index=['A484', 'C012', 'A123', 'B663', 'A377'],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37318480",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe1c5739",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "dedad6f3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "e31f21c6",
"metadata": {},
"source": [
"## DataFrame attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4109f1eb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "708f9bb5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cb2f33b9",
"metadata": {},
"source": [
"## Indexing rows and columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19ef2738",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f354ffc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "94563f03",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "43ab5233",
"metadata": {},
"source": [
"## Examining a column"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2cb544c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "86388f86",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "fc081b90",
"metadata": {},
"source": [
"# Filtering"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "263ae06c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "318da062",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a570023a",
"metadata": {},
"source": [
"# Basic operations are by column (unlike NumPy)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7260d212",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "49b7057a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5a0f053",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e1ffe32",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7cf9b5d7",
"metadata": {},
"source": [
"# Operations on strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b78bc237",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0236069f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5761725b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce3d54ad",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "8c5584db",
"metadata": {},
"source": [
"# Adding new columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6e09176",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9a552f0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e354ace",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,462 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "37957eb0",
"metadata": {},
"source": [
"# Combine information across tables: joins and anti-joins"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b6f949f7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "6a7fcf90",
"metadata": {},
"source": [
"# \"Load\" some experimental data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a9450803",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>711</td>\n",
" <td>A2</td>\n",
" <td>0.44</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 711 A1 4.01 RIGHT\n",
"4 711 A2 0.44 LEFT\n",
"5 313 A1 0.07 RIGHT\n",
"6 313 B1 0.08 RIGHT\n",
"7 712 A2 3.29 LEFT\n",
"8 314 A2 0.29 LEFT\n",
"9 714 B2 3.32 RIGHT\n",
"10 314 B1 0.14 RIGHT\n",
"11 314 C2 0.73 RIGHT\n",
"12 713 B1 5.74 LEFT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['711', 'A2', 0.44, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "9f6de0d6",
"metadata": {},
"source": [
"Each experiment belongs to one experimental condition, but the parameters of each condition are not in the table"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "455471d7",
"metadata": {},
"outputs": [],
"source": [
"condition_to_orientation = {\n",
" 'A1': 0,\n",
" 'A2': 0,\n",
" 'B1': 45,\n",
" 'B2': 45,\n",
" 'C1': 90,\n",
"}\n",
"\n",
"condition_to_duration = {\n",
" 'A1': 0.1,\n",
" 'A2': 0.01,\n",
" 'B1': 0.1,\n",
" 'B2': 0.01,\n",
" 'C1': 0.2,\n",
"}\n",
"\n",
"condition_to_surround = {\n",
" 'A1': 'FULL',\n",
" 'A2': 'NONE',\n",
" 'B1': 'NONE',\n",
" 'B2': 'FULL',\n",
" 'C1': 'FULL',\n",
"}\n",
"\n",
"\n",
"condition_to_stimulus_type = {\n",
" 'A1': 'LINES',\n",
" 'A2': 'DOTS',\n",
" 'B1': 'PLAID',\n",
" 'B2': 'PLAID',\n",
" 'C1': 'WIGGLES',\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "5ccfd7e7",
"metadata": {},
"source": [
"# Manually adding the condition parameters to the table"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "cc32110c",
"metadata": {},
"outputs": [],
"source": [
"data_with_properties = data.copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06263dc6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b96962b2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "d6e71b13",
"metadata": {},
"source": [
"# Using a join operation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d9835d7c",
"metadata": {},
"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>orientation</th>\n",
" <th>duration</th>\n",
" <th>surround</th>\n",
" <th>stimulus_type</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0</td>\n",
" <td>0.1</td>\n",
" <td>FULL</td>\n",
" <td>LINES</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>0</td>\n",
" <td>0.01</td>\n",
" <td>NONE</td>\n",
" <td>DOTS</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>45</td>\n",
" <td>0.1</td>\n",
" <td>NONE</td>\n",
" <td>PLAID</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>45</td>\n",
" <td>0.01</td>\n",
" <td>FULL</td>\n",
" <td>PLAID</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C1</th>\n",
" <td>90</td>\n",
" <td>0.2</td>\n",
" <td>FULL</td>\n",
" <td>WIGGLES</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" orientation duration surround stimulus_type\n",
"A1 0 0.1 FULL LINES\n",
"A2 0 0.01 NONE DOTS\n",
"B1 45 0.1 NONE PLAID\n",
"B2 45 0.01 FULL PLAID\n",
"C1 90 0.2 FULL WIGGLES"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Often, this is done using a spreadsheet\n",
"condition_properties = pd.DataFrame(\n",
" [condition_to_orientation, condition_to_duration, condition_to_surround, condition_to_stimulus_type],\n",
" index=['orientation', 'duration', 'surround', 'stimulus_type'],\n",
").T\n",
"condition_properties"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c27ea9f3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e563cd0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "cba9534f",
"metadata": {},
"source": [
"# Anti-join: filter out unwanted data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1cb2bbdb",
"metadata": {},
"outputs": [],
"source": [
"# We are given a list of subjects that are outliers and should be disregarded in the analysis\n",
"outliers = pd.DataFrame([['711'], ['712'], ['713'], ['714'], ['888']], columns=['subject_id'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0e2c3c5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "90d92640",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,814 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Split-apply-combine operations for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba193f3f",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 313 A1 0.07 RIGHT\n",
"4 313 B1 0.08 RIGHT\n",
"5 314 A2 0.29 LEFT\n",
"6 314 B1 0.14 RIGHT\n",
"7 314 C2 0.73 RIGHT\n",
"8 711 A1 4.01 RIGHT\n",
"9 712 A2 3.29 LEFT\n",
"10 713 B1 5.74 LEFT\n",
"11 714 B2 3.32 RIGHT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "8a239e0c",
"metadata": {},
"source": [
"# Group-by"
]
},
{
"cell_type": "markdown",
"id": "31eba91e",
"metadata": {},
"source": [
"We want to compute the mean response time by condition.\n",
"\n",
"Let's start by doing it by hand, using for loops!"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e8331039",
"metadata": {},
"outputs": [],
"source": [
"conditions = data['condition_id'].unique()\n",
"results_dict = {}\n",
"for condition in conditions:\n",
" group = data[data['condition_id'] == condition]\n",
" results_dict[condition] = group['response_time'].mean()\n",
"\n",
"results = pd.DataFrame([results_dict], index=['response_time']).T"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "09cb04c4",
"metadata": {},
"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>response_time</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>1.400000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.705000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>1.986667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>3.320000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" response_time\n",
"A1 1.400000\n",
"A2 1.316667\n",
"C2 0.705000\n",
"B1 1.986667\n",
"B2 3.320000"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results"
]
},
{
"cell_type": "markdown",
"id": "2bc09c66",
"metadata": {},
"source": [
"This is a basic operation, and we would need to repeat his pattern a million times!\n",
"\n",
"Pandas and all other tools for tabular data provide a command for performing operations on groups."
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "0500cd4a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pandas.core.groupby.generic.DataFrameGroupBy object at 0x14ff67a90>"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# df.groupby(column_name) groups a DataFrame by the values in the column\n",
"data.groupby('condition_id')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c5857c4e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 3\n",
"A2 3\n",
"B1 3\n",
"B2 1\n",
"C2 2\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The group-by object can by used as a DataFrame. \n",
"# Operations are executed on each group individually, then aggregated\n",
"data.groupby('condition_id').size()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "5c865cc1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 1.400000\n",
"A2 1.316667\n",
"B1 1.986667\n",
"B2 3.320000\n",
"C2 0.705000\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.groupby('condition_id')['response_time'].mean()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "615a4515",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id\n",
"A1 4.01\n",
"A2 3.29\n",
"B1 5.74\n",
"B2 3.32\n",
"C2 0.73\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.groupby('condition_id')['response_time'].max()"
]
},
{
"cell_type": "markdown",
"id": "b0441458",
"metadata": {},
"source": [
"# Pivot tables"
]
},
{
"cell_type": "markdown",
"id": "3feec98d",
"metadata": {},
"source": [
"We want to look at response time biases when the subjects respond LEFT vs RIGHT. In principle, we expect them to have the same response time in both cases.\n",
"\n",
"We compute a summary table with 1) condition_id on the rows; 2) response on the columns; 3) the average response time for all experiments with a that condition and response\n",
"\n",
"We can do it with `groupby`, with some table manipulation commands."
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "4a8a7d0d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"condition_id response\n",
"A1 LEFT 0.120000\n",
" RIGHT 2.040000\n",
"A2 LEFT 1.316667\n",
"B1 LEFT 5.740000\n",
" RIGHT 0.110000\n",
"B2 RIGHT 3.320000\n",
"C2 LEFT 0.680000\n",
" RIGHT 0.730000\n",
"Name: response_time, dtype: float64"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary = data.groupby(['condition_id', 'response'])['response_time'].mean()\n",
"summary"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "e5a645e0",
"metadata": {},
"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>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"response LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04\n",
"A2 1.316667 NaN\n",
"B1 5.740000 0.11\n",
"B2 NaN 3.32\n",
"C2 0.680000 0.73"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary.unstack(level=1)"
]
},
{
"cell_type": "markdown",
"id": "3307fcc6",
"metadata": {},
"source": [
"Pandas has a command called `pivot_table` that can be used to perform this kind of operation straightforwardly."
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "8941edfe",
"metadata": {},
"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>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"response LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04\n",
"A2 1.316667 NaN\n",
"B1 5.740000 0.11\n",
"B2 NaN 3.32\n",
"C2 0.680000 0.73"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.pivot_table(index='condition_id', columns='response', values='response_time', aggfunc='mean')"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "a7d1d998",
"metadata": {},
"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 tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">mean</th>\n",
" <th colspan=\"2\" halign=\"left\">std</th>\n",
" <th colspan=\"2\" halign=\"left\">count</th>\n",
" </tr>\n",
" <tr>\n",
" <th>response</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" <th>LEFT</th>\n",
" <th>RIGHT</th>\n",
" </tr>\n",
" <tr>\n",
" <th>condition_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>0.120000</td>\n",
" <td>2.04</td>\n",
" <td>NaN</td>\n",
" <td>2.786001</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>1.316667</td>\n",
" <td>NaN</td>\n",
" <td>1.709425</td>\n",
" <td>NaN</td>\n",
" <td>3.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B1</th>\n",
" <td>5.740000</td>\n",
" <td>0.11</td>\n",
" <td>NaN</td>\n",
" <td>0.042426</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B2</th>\n",
" <td>NaN</td>\n",
" <td>3.32</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C2</th>\n",
" <td>0.680000</td>\n",
" <td>0.73</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" mean std count \n",
"response LEFT RIGHT LEFT RIGHT LEFT RIGHT\n",
"condition_id \n",
"A1 0.120000 2.04 NaN 2.786001 1.0 2.0\n",
"A2 1.316667 NaN 1.709425 NaN 3.0 NaN\n",
"B1 5.740000 0.11 NaN 0.042426 1.0 2.0\n",
"B2 NaN 3.32 NaN NaN NaN 1.0\n",
"C2 0.680000 0.73 NaN NaN 1.0 1.0"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(\n",
" data\n",
" .pivot_table(\n",
" index='condition_id', \n",
" columns='response', \n",
" values='response_time', \n",
" aggfunc=['mean', 'std', 'count'],\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a770b812",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c77c2dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,335 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Split-apply-combine operations for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba193f3f",
"metadata": {},
"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>subject_id</th>\n",
" <th>condition_id</th>\n",
" <th>response_time</th>\n",
" <th>response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>312</td>\n",
" <td>A1</td>\n",
" <td>0.12</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>312</td>\n",
" <td>A2</td>\n",
" <td>0.37</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>312</td>\n",
" <td>C2</td>\n",
" <td>0.68</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>313</td>\n",
" <td>A1</td>\n",
" <td>0.07</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>313</td>\n",
" <td>B1</td>\n",
" <td>0.08</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>314</td>\n",
" <td>A2</td>\n",
" <td>0.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>314</td>\n",
" <td>B1</td>\n",
" <td>0.14</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>314</td>\n",
" <td>C2</td>\n",
" <td>0.73</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>711</td>\n",
" <td>A1</td>\n",
" <td>4.01</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>712</td>\n",
" <td>A2</td>\n",
" <td>3.29</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>713</td>\n",
" <td>B1</td>\n",
" <td>5.74</td>\n",
" <td>LEFT</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>714</td>\n",
" <td>B2</td>\n",
" <td>3.32</td>\n",
" <td>RIGHT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id condition_id response_time response\n",
"0 312 A1 0.12 LEFT\n",
"1 312 A2 0.37 LEFT\n",
"2 312 C2 0.68 LEFT\n",
"3 313 A1 0.07 RIGHT\n",
"4 313 B1 0.08 RIGHT\n",
"5 314 A2 0.29 LEFT\n",
"6 314 B1 0.14 RIGHT\n",
"7 314 C2 0.73 RIGHT\n",
"8 711 A1 4.01 RIGHT\n",
"9 712 A2 3.29 LEFT\n",
"10 713 B1 5.74 LEFT\n",
"11 714 B2 3.32 RIGHT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame(\n",
" data=[\n",
" ['312', 'A1', 0.12, 'LEFT'],\n",
" ['312', 'A2', 0.37, 'LEFT'],\n",
" ['312', 'C2', 0.68, 'LEFT'],\n",
" ['313', 'A1', 0.07, 'RIGHT'],\n",
" ['313', 'B1', 0.08, 'RIGHT'],\n",
" ['314', 'A2', 0.29, 'LEFT'],\n",
" ['314', 'B1', 0.14, 'RIGHT'],\n",
" ['314', 'C2', 0.73, 'RIGHT'],\n",
" ['711', 'A1', 4.01, 'RIGHT'],\n",
" ['712', 'A2', 3.29, 'LEFT'],\n",
" ['713', 'B1', 5.74, 'LEFT'],\n",
" ['714', 'B2', 3.32, 'RIGHT'],\n",
" ],\n",
" columns=['subject_id', 'condition_id', 'response_time', 'response'],\n",
")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "8a239e0c",
"metadata": {},
"source": [
"# Group-by"
]
},
{
"cell_type": "markdown",
"id": "31eba91e",
"metadata": {},
"source": [
"We want to compute the mean response time by condition.\n",
"\n",
"Let's start by doing it by hand, using for loops!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff3f890b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "805d04c7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "2bc09c66",
"metadata": {},
"source": [
"This is a basic operation, and we would need to repeat his pattern a million times!\n",
"\n",
"Pandas and all other tools for tabular data provide a command for performing operations on groups."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcc8c9c7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "818b8346",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b0441458",
"metadata": {},
"source": [
"# Pivot tables"
]
},
{
"cell_type": "markdown",
"id": "3feec98d",
"metadata": {},
"source": [
"We want to look at response time biases when the subjects respond LEFT vs RIGHT. In principle, we expect them to have the same response time in both cases.\n",
"\n",
"We compute a summary table with 1) condition_id on the rows; 2) response on the columns; 3) the average response time for all experiments with a that condition and response\n",
"\n",
"We can do it with `groupby`, with some table manipulation commands."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04f6ff60",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "62600721",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "3307fcc6",
"metadata": {},
"source": [
"Pandas has a command called `pivot_table` that can be used to perform this kind of operation straightforwardly."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a770b812",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c77c2dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,320 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "247bbf84",
"metadata": {},
"source": [
"# Window functions for tabular data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44584190",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "83bbd275",
"metadata": {},
"source": [
"# Load experimental data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "88b9e189",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('timed_responses.csv', index_col=0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "987a3518",
"metadata": {},
"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>subject_id</th>\n",
" <th>time (ms)</th>\n",
" <th>response</th>\n",
" <th>accuracy</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>574</th>\n",
" <td>3</td>\n",
" <td>540</td>\n",
" <td>RIGHT</td>\n",
" <td>0.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1190</th>\n",
" <td>2</td>\n",
" <td>552</td>\n",
" <td>LEFT</td>\n",
" <td>0.43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1895</th>\n",
" <td>2</td>\n",
" <td>1036</td>\n",
" <td>LEFT</td>\n",
" <td>0.36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>53</th>\n",
" <td>3</td>\n",
" <td>257</td>\n",
" <td>RIGHT</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>158</th>\n",
" <td>2</td>\n",
" <td>743</td>\n",
" <td>RIGHT</td>\n",
" <td>0.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>551</th>\n",
" <td>3</td>\n",
" <td>619</td>\n",
" <td>LEFT</td>\n",
" <td>0.25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1602</th>\n",
" <td>1</td>\n",
" <td>43</td>\n",
" <td>RIGHT</td>\n",
" <td>0.65</td>\n",
" </tr>\n",
" <tr>\n",
" <th>413</th>\n",
" <td>1</td>\n",
" <td>471</td>\n",
" <td>LEFT</td>\n",
" <td>0.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>785</th>\n",
" <td>1</td>\n",
" <td>121</td>\n",
" <td>LEFT</td>\n",
" <td>0.10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1393</th>\n",
" <td>2</td>\n",
" <td>903</td>\n",
" <td>RIGHT</td>\n",
" <td>0.33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>629</th>\n",
" <td>2</td>\n",
" <td>353</td>\n",
" <td>LEFT</td>\n",
" <td>0.17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1829</th>\n",
" <td>3</td>\n",
" <td>768</td>\n",
" <td>RIGHT</td>\n",
" <td>0.26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>902</th>\n",
" <td>1</td>\n",
" <td>1093</td>\n",
" <td>LEFT</td>\n",
" <td>0.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1486</th>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>RIGHT</td>\n",
" <td>0.29</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject_id time (ms) response accuracy\n",
"574 3 540 RIGHT 0.04\n",
"1190 2 552 LEFT 0.43\n",
"1895 2 1036 LEFT 0.36\n",
"53 3 257 RIGHT 0.11\n",
"158 2 743 RIGHT 0.32\n",
"551 3 619 LEFT 0.25\n",
"1602 1 43 RIGHT 0.65\n",
"413 1 471 LEFT 0.80\n",
"785 1 121 LEFT 0.10\n",
"1393 2 903 RIGHT 0.33\n",
"629 2 353 LEFT 0.17\n",
"1829 3 768 RIGHT 0.26\n",
"902 1 1093 LEFT 0.34\n",
"1486 2 3 RIGHT 0.29"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"id": "5c41cd93",
"metadata": {},
"source": [
"# Split-apply-combine operations return one aggregated value per group"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0234ccf2",
"metadata": {},
"outputs": [],
"source": [
"df.groupby('subject_id')['accuracy'].max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b2a1796",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "2bb99152",
"metadata": {},
"source": [
"# However, for some calculations we need to have a value per row\n",
"\n",
"For example: for each subject, rank the responses by decreasing accuracy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3aed0755",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "17f3d40f",
"metadata": {},
"source": [
"# In many cases, a window functions is combined with a sorting operation\n",
"\n",
"For example: for each subject, count the number of \"LEFT\" responses up until any moment in the experiment"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67efdd56",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a00b4f39",
"metadata": {},
"source": [
"# Window functions are also useful to compute changes in the data for each group\n",
"\n",
"In this case, the window function often uses the `shift(n)` method that lags the data by `n` rows"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e553c17f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2973e3d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ca46b0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,15 @@
,subject_id,time (ms),response,accuracy
574,3,540,RIGHT,0.04
1190,2,552,LEFT,0.43
1895,2,1036,LEFT,0.36
53,3,257,RIGHT,0.11
158,2,743,RIGHT,0.32
551,3,619,LEFT,0.25
1602,1,43,RIGHT,0.65
413,1,471,LEFT,0.8
785,1,121,LEFT,0.1
1393,2,903,RIGHT,0.33
629,2,353,LEFT,0.17
1829,3,768,RIGHT,0.26
902,1,1093,LEFT,0.34
1486,2,3,RIGHT,0.29
1 subject_id time (ms) response accuracy
2 574 3 540 RIGHT 0.04
3 1190 2 552 LEFT 0.43
4 1895 2 1036 LEFT 0.36
5 53 3 257 RIGHT 0.11
6 158 2 743 RIGHT 0.32
7 551 3 619 LEFT 0.25
8 1602 1 43 RIGHT 0.65
9 413 1 471 LEFT 0.8
10 785 1 121 LEFT 0.1
11 1393 2 903 RIGHT 0.33
12 629 2 353 LEFT 0.17
13 1829 3 768 RIGHT 0.26
14 902 1 1093 LEFT 0.34
15 1486 2 3 RIGHT 0.29