Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created March 17, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ischurov/76d86f8f89cd5299fab2298a63cdc236 to your computer and use it in GitHub Desktop.
Save ischurov/76d86f8f89cd5299fab2298a63cdc236 to your computer and use it in GitHub Desktop.
Untitled.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Наука о данных\n### Совместный бакалавриат ВШЭ-РЭШ, 2021-2022 учебный год\n_Илья Щуров_\n\n[Страница курса](http://math-info.hse.ru/s21/j)"
},
{
"metadata": {
"trusted": false
},
"id": "duplicate-mining",
"cell_type": "code",
"source": "import pandas as pd",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "heated-digest",
"cell_type": "code",
"source": "gradebook = pd.DataFrame([['Alice', 'Algebra', 4],\n ['Alice', 'Geometry', 5],\n ['Bob', 'Algebra', 3],\n ['Bob', 'Geometry', 5],\n ['Claudia', 'Algebra', 4],\n ['Claudia', 'Geometry', 3]\n ],\n columns=['student', 'course', 'grade']\n )",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "english-communication",
"cell_type": "code",
"source": "gradebook",
"execution_count": 7,
"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>student</th>\n <th>course</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Alice</td>\n <td>Algebra</td>\n <td>4</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Alice</td>\n <td>Geometry</td>\n <td>5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Bob</td>\n <td>Algebra</td>\n <td>3</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Bob</td>\n <td>Geometry</td>\n <td>5</td>\n </tr>\n <tr>\n <th>4</th>\n <td>Claudia</td>\n <td>Algebra</td>\n <td>4</td>\n </tr>\n <tr>\n <th>5</th>\n <td>Claudia</td>\n <td>Geometry</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student course grade\n0 Alice Algebra 4\n1 Alice Geometry 5\n2 Bob Algebra 3\n3 Bob Geometry 5\n4 Claudia Algebra 4\n5 Claudia Geometry 3"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "blessed-position",
"cell_type": "code",
"source": "(gradebook\n .pivot(index='student', columns='course', values='grade')\n .query('Geometry > Algebra')\n .index\n)",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "Index(['Alice', 'Bob'], dtype='object', name='student')"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "specified-pioneer",
"cell_type": "code",
"source": "(gradebook\n .pivot(index='student', columns='course', values='grade')\n [lambda x: x['Geometry'] > x['Algebra']]\n .index\n)",
"execution_count": 11,
"outputs": [
{
"data": {
"text/plain": "Index(['Alice', 'Bob'], dtype='object', name='student')"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "olive-crown",
"cell_type": "code",
"source": "(gradebook\n .pivot(index='student', columns='course', values='grade')\n .mean(axis=0)\n)",
"execution_count": 13,
"outputs": [
{
"data": {
"text/plain": "course\nAlgebra 3.666667\nGeometry 4.333333\ndtype: float64"
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "native-nomination",
"cell_type": "code",
"source": "(gradebook\n .pivot(index='student', columns='course', values='grade')\n .mean(axis=1)\n)",
"execution_count": 14,
"outputs": [
{
"data": {
"text/plain": "student\nAlice 4.5\nBob 4.0\nClaudia 3.5\ndtype: float64"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "placed-attribute",
"cell_type": "code",
"source": "gradebook.groupby('course')['grade'].mean()",
"execution_count": 16,
"outputs": [
{
"data": {
"text/plain": "course\nAlgebra 3.666667\nGeometry 4.333333\nName: grade, dtype: float64"
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "buried-yukon",
"cell_type": "code",
"source": "gradebook = pd.DataFrame([['Alice', 'Algebra', 4],\n ['Alice', 'Geometry', 5],\n ['Bob', 'Algebra', 3],\n ['Claudia', 'Algebra', 4],\n ['Claudia', 'Geometry', 3]\n ],\n columns=['student', 'course', 'grade']\n )",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "limited-travel",
"cell_type": "code",
"source": "gradebook.pivot(index='student', columns='course', values='grade')",
"execution_count": 21,
"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>course</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4.0</td>\n <td>5.0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>3.0</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>Claudia</th>\n <td>4.0</td>\n <td>3.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course Algebra Geometry\nstudent \nAlice 4.0 5.0\nBob 3.0 NaN\nClaudia 4.0 3.0"
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "starting-accuracy",
"cell_type": "code",
"source": "(gradebook.pivot(index='student', columns='course', values='grade')\n .mean(axis=0))",
"execution_count": 20,
"outputs": [
{
"data": {
"text/plain": "course\nAlgebra 3.666667\nGeometry 4.000000\ndtype: float64"
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "positive-kingston",
"cell_type": "code",
"source": "(gradebook.groupby('course')['grade'].mean())",
"execution_count": 23,
"outputs": [
{
"data": {
"text/plain": "course\nAlgebra 3.666667\nGeometry 4.000000\nName: grade, dtype: float64"
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "assured-lambda",
"cell_type": "code",
"source": "gradebook = pd.DataFrame([['Alice', 'Algebra', 4],\n ['Alice', 'Geometry', 5],\n ['Bob', 'Algebra', 3],\n ['Bob', 'Geometry', 4],\n ['Bob', 'Geometry', 5],\n ['Claudia', 'Algebra', 4],\n ['Claudia', 'Geometry', 3]\n ],\n columns=['student', 'course', 'grade']\n )",
"execution_count": 24,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "respected-spectrum",
"cell_type": "code",
"source": "(gradebook.pivot(index='student', columns='course', values='grade'))",
"execution_count": 25,
"outputs": [
{
"ename": "ValueError",
"evalue": "Index contains duplicate entries, cannot reshape",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_34981/2306497833.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mgradebook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'student'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'course'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'grade'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(self, index, columns, values)\u001b[0m\n\u001b[1;32m 7791\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7792\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 7793\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7794\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7795\u001b[0m _shared_docs[\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/reshape/pivot.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(data, index, columns, values)\u001b[0m\n\u001b[1;32m 515\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 516\u001b[0m \u001b[0mindexed\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_constructor_sliced\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmultiindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 517\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mindexed\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumns_listlike\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 518\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 519\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36munstack\u001b[0;34m(self, level, fill_value)\u001b[0m\n\u001b[1;32m 4079\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0munstack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4080\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4081\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0munstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4082\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4083\u001b[0m \u001b[0;31m# ----------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36munstack\u001b[0;34m(obj, level, fill_value)\u001b[0m\n\u001b[1;32m 458\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_1d_only_ea_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 459\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_unstack_extension_series\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 460\u001b[0;31m unstacker = _Unstacker(\n\u001b[0m\u001b[1;32m 461\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconstructor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_constructor_expanddim\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 462\u001b[0m )\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, index, level, constructor)\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Unstacked DataFrame is too big, causing int32 overflow\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 133\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_selectors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 134\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mcache_readonly\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36m_make_selectors\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 183\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 184\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 185\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Index contains duplicate entries, cannot reshape\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 186\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 187\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup_index\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcomp_index\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Index contains duplicate entries, cannot reshape"
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "hairy-retention",
"cell_type": "code",
"source": "(gradebook.pivot_table(\n index='student', \n columns='course', \n values='grade', aggfunc='last'))",
"execution_count": 27,
"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>course</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Claudia</th>\n <td>4</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course Algebra Geometry\nstudent \nAlice 4 5\nBob 3 5\nClaudia 4 3"
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "nonprofit-championship",
"cell_type": "code",
"source": "grades = pd.DataFrame([['Alice', 3, 4],\n ['Bob', 4, 5]],\n columns=['student', 'Algebra', 'Geometry']\n )",
"execution_count": 31,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "outside-guitar",
"cell_type": "code",
"source": "grades",
"execution_count": 35,
"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>student</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Alice</td>\n <td>3</td>\n <td>4</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Bob</td>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student Algebra Geometry\n0 Alice 3 4\n1 Bob 4 5"
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "tender-ratio",
"cell_type": "code",
"source": "grades.melt(id_vars='student', var_name='course', value_name='grade')",
"execution_count": 34,
"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>student</th>\n <th>course</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Alice</td>\n <td>Algebra</td>\n <td>3</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Bob</td>\n <td>Algebra</td>\n <td>4</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Alice</td>\n <td>Geometry</td>\n <td>4</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Bob</td>\n <td>Geometry</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student course grade\n0 Alice Algebra 3\n1 Bob Algebra 4\n2 Alice Geometry 4\n3 Bob Geometry 5"
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "guilty-government",
"cell_type": "code",
"source": "(gradebook.pivot_table(\n index='student', \n columns='course', \n values='grade', aggfunc='last')\n)",
"execution_count": 38,
"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>course</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Claudia</th>\n <td>4</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course Algebra Geometry\nstudent \nAlice 4 5\nBob 3 5\nClaudia 4 3"
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "unnecessary-miller",
"cell_type": "code",
"source": "(gradebook.pivot_table(\n index='student', \n columns='course', \n values='grade', aggfunc='last')\n .reset_index()\n)",
"execution_count": 37,
"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>course</th>\n <th>student</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Alice</td>\n <td>4</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Bob</td>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Claudia</td>\n <td>4</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course student Algebra Geometry\n0 Alice 4 5\n1 Bob 3 5\n2 Claudia 4 3"
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "corrected-advocacy",
"cell_type": "code",
"source": "(gradebook.pivot_table(\n index='student', \n columns='course', \n values='grade', aggfunc='last')\n .reset_index()\n .set_index('student')\n)",
"execution_count": 39,
"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>course</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>Claudia</th>\n <td>4</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course Algebra Geometry\nstudent \nAlice 4 5\nBob 3 5\nClaudia 4 3"
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "micro-soundtrack",
"cell_type": "code",
"source": "(gradebook.pivot_table(\n index='student', \n columns='course', \n values='grade', aggfunc='last')\n .reset_index(drop=True)\n)",
"execution_count": 40,
"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>course</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>4</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>4</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": "course Algebra Geometry\n0 4 5\n1 3 5\n2 4 3"
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "brave-interaction",
"cell_type": "code",
"source": "students = pd.DataFrame(\n [['Winnie', 4323],\n ['Piglet', 1242],\n ['Owl', 123]],\n columns=['student', 'idx']\n)",
"execution_count": 41,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "marine-affiliation",
"cell_type": "code",
"source": "students",
"execution_count": 42,
"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>student</th>\n <th>idx</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Owl</td>\n <td>123</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx\n0 Winnie 4323\n1 Piglet 1242\n2 Owl 123"
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "minute-limit",
"cell_type": "code",
"source": "grades = pd.DataFrame([[1242, 5],\n [153, 3],\n [4323, 4]],\n columns=['student_id', 'grade'])",
"execution_count": 45,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "compound-atlantic",
"cell_type": "code",
"source": "grades",
"execution_count": 46,
"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>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1242</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>153</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>4323</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student_id grade\n0 1242 5\n1 153 3\n2 4323 4"
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "super-split",
"cell_type": "code",
"source": "students.merge(grades, left_on='idx', right_on='student_id')",
"execution_count": 47,
"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>student</th>\n <th>idx</th>\n <th>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>4323</td>\n <td>4</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242</td>\n <td>1242</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx student_id grade\n0 Winnie 4323 4323 4\n1 Piglet 1242 1242 5"
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "unnecessary-indiana",
"cell_type": "code",
"source": "students.merge(grades, left_on='idx', right_on='student_id', how='left')",
"execution_count": 48,
"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>student</th>\n <th>idx</th>\n <th>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>4323.0</td>\n <td>4.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242</td>\n <td>1242.0</td>\n <td>5.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Owl</td>\n <td>123</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx student_id grade\n0 Winnie 4323 4323.0 4.0\n1 Piglet 1242 1242.0 5.0\n2 Owl 123 NaN NaN"
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "connected-ethernet",
"cell_type": "code",
"source": "students.merge(grades, left_on='idx', right_on='student_id', how='right')",
"execution_count": 49,
"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>student</th>\n <th>idx</th>\n <th>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Piglet</td>\n <td>1242.0</td>\n <td>1242</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>NaN</td>\n <td>NaN</td>\n <td>153</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Winnie</td>\n <td>4323.0</td>\n <td>4323</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx student_id grade\n0 Piglet 1242.0 1242 5\n1 NaN NaN 153 3\n2 Winnie 4323.0 4323 4"
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "alternate-moment",
"cell_type": "code",
"source": "students.merge(grades, left_on='idx', right_on='student_id', how='outer')",
"execution_count": 50,
"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>student</th>\n <th>idx</th>\n <th>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323.0</td>\n <td>4323.0</td>\n <td>4.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242.0</td>\n <td>1242.0</td>\n <td>5.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Owl</td>\n <td>123.0</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>3</th>\n <td>NaN</td>\n <td>NaN</td>\n <td>153.0</td>\n <td>3.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx student_id grade\n0 Winnie 4323.0 4323.0 4.0\n1 Piglet 1242.0 1242.0 5.0\n2 Owl 123.0 NaN NaN\n3 NaN NaN 153.0 3.0"
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "authentic-dryer",
"cell_type": "code",
"source": "students = pd.DataFrame(\n [['Winnie', 4323],\n ['Piglet', 1242],\n ['Winnie the Pooh', 4323],\n ['Owl', 123]],\n columns=['student', 'idx']\n)",
"execution_count": 54,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "partial-album",
"cell_type": "code",
"source": "grades = pd.DataFrame([[1242, 5],\n [153, 3],\n [4323, 4],\n [4323, 3],\n ],\n columns=['student_id', 'grade'])",
"execution_count": 57,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "arabic-federation",
"cell_type": "code",
"source": "students.merge(grades, left_on='idx', right_on='student_id')",
"execution_count": 58,
"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>student</th>\n <th>idx</th>\n <th>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>4323</td>\n <td>4</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>4323</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Winnie the Pooh</td>\n <td>4323</td>\n <td>4323</td>\n <td>4</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Winnie the Pooh</td>\n <td>4323</td>\n <td>4323</td>\n <td>3</td>\n </tr>\n <tr>\n <th>4</th>\n <td>Piglet</td>\n <td>1242</td>\n <td>1242</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx student_id grade\n0 Winnie 4323 4323 4\n1 Winnie 4323 4323 3\n2 Winnie the Pooh 4323 4323 4\n3 Winnie the Pooh 4323 4323 3\n4 Piglet 1242 1242 5"
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "driven-independence",
"cell_type": "code",
"source": "grades",
"execution_count": 63,
"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>student_id</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1242</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>153</td>\n <td>3</td>\n </tr>\n <tr>\n <th>2</th>\n <td>4323</td>\n <td>4</td>\n </tr>\n <tr>\n <th>3</th>\n <td>4323</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student_id grade\n0 1242 5\n1 153 3\n2 4323 4\n3 4323 3"
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "devoted-concentrate",
"cell_type": "code",
"source": "students.join(grades.set_index('student_id'), on='idx')",
"execution_count": 71,
"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>student</th>\n <th>idx</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>4.0</td>\n </tr>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n <td>3.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242</td>\n <td>5.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Winnie the Pooh</td>\n <td>4323</td>\n <td>4.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Winnie the Pooh</td>\n <td>4323</td>\n <td>3.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Owl</td>\n <td>123</td>\n <td>NaN</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx grade\n0 Winnie 4323 4.0\n0 Winnie 4323 3.0\n1 Piglet 1242 5.0\n2 Winnie the Pooh 4323 4.0\n2 Winnie the Pooh 4323 3.0\n3 Owl 123 NaN"
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "backed-blank",
"cell_type": "code",
"source": "students",
"execution_count": 72,
"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>student</th>\n <th>idx</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>4323</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Piglet</td>\n <td>1242</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Winnie the Pooh</td>\n <td>4323</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Owl</td>\n <td>123</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student idx\n0 Winnie 4323\n1 Piglet 1242\n2 Winnie the Pooh 4323\n3 Owl 123"
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "square-humanity",
"cell_type": "code",
"source": "students = pd.DataFrame([['Winnie', 'Pooh', 3],\n ['Winnie', 'Not Pooh', 4],\n ['Christofer', 'Robin', 7]\n ],\n columns=['first_name', 'second_name', 'age']\n )",
"execution_count": 82,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "metropolitan-toronto",
"cell_type": "code",
"source": "grades = pd.DataFrame([['Winnie', 'Pooh', 5],\n ['Christofer', 'Robin', 4]\n ],\n columns=['first_name', 'second_name', 'grade']\n )",
"execution_count": 83,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "compact-semiconductor",
"cell_type": "code",
"source": "students.merge(grades, on=['first_name', 'second_name'])",
"execution_count": 84,
"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>first_name</th>\n <th>second_name</th>\n <th>age</th>\n <th>grade</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>Pooh</td>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Christofer</td>\n <td>Robin</td>\n <td>7</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " first_name second_name age grade\n0 Winnie Pooh 3 5\n1 Christofer Robin 7 4"
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "published-hierarchy",
"cell_type": "code",
"source": "students = pd.DataFrame([['Winnie', 'Pooh', 3],\n ['Winnie', 'Not Pooh', 4],\n ['Christofer', 'Robin', 7]\n ],\n columns=['first_name', 'second_name', 'grade']\n )",
"execution_count": 85,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "threatened-newspaper",
"cell_type": "code",
"source": "grades = pd.DataFrame([['Winnie', 'Pooh', 5],\n ['Christofer', 'Robin', 4]\n ],\n columns=['first_name', 'second_name', 'grade']\n )",
"execution_count": 86,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "flush-criterion",
"cell_type": "code",
"source": "students.merge(grades, on=['first_name', 'second_name'], \n suffixes=('_algebra', '_calculus'))",
"execution_count": 88,
"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>first_name</th>\n <th>second_name</th>\n <th>grade_algebra</th>\n <th>grade_calculus</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Winnie</td>\n <td>Pooh</td>\n <td>3</td>\n <td>5</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Christofer</td>\n <td>Robin</td>\n <td>7</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " first_name second_name grade_algebra grade_calculus\n0 Winnie Pooh 3 5\n1 Christofer Robin 7 4"
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "proprietary-polish",
"cell_type": "code",
"source": "grades = pd.DataFrame([['Alice', 3, 4],\n ['Bob', 4, 5]],\n columns=['student', 'Algebra', 'Geometry']\n )",
"execution_count": 89,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "technological-chinese",
"cell_type": "code",
"source": "grades",
"execution_count": 92,
"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>student</th>\n <th>Algebra</th>\n <th>Geometry</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Alice</td>\n <td>3</td>\n <td>4</td>\n </tr>\n <tr>\n <th>1</th>\n <td>Bob</td>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " student Algebra Geometry\n0 Alice 3 4\n1 Bob 4 5"
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "promising-sherman",
"cell_type": "code",
"source": "grades.set_index('student', inplace=True)",
"execution_count": 94,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "charitable-thriller",
"cell_type": "code",
"source": "grades",
"execution_count": 95,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>3</td>\n <td>4</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice 3 4\nBob 4 5"
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "literary-validation",
"cell_type": "code",
"source": "grades[grades['Geometry'] >= 5]",
"execution_count": 98,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Bob</th>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nBob 4 5"
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "hydraulic-treatment",
"cell_type": "code",
"source": "grades[grades >= 5].fillna(-1)",
"execution_count": 101,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>-1.0</td>\n <td>-1.0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>-1.0</td>\n <td>5.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice -1.0 -1.0\nBob -1.0 5.0"
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "dedicated-cardiff",
"cell_type": "code",
"source": "grades.mean()",
"execution_count": 103,
"outputs": [
{
"data": {
"text/plain": "Algebra 3.5\nGeometry 4.5\ndtype: float64"
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "martial-there",
"cell_type": "code",
"source": "grades",
"execution_count": 104,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>3</td>\n <td>4</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice 3 4\nBob 4 5"
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "norman-emergency",
"cell_type": "code",
"source": "grades[grades >= 5].fillna(grades.mean())",
"execution_count": 102,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>3.5</td>\n <td>4.5</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>3.5</td>\n <td>5.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice 3.5 4.5\nBob 3.5 5.0"
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "dynamic-witness",
"cell_type": "code",
"source": "grades[grades >= 5].fillna(-grades)",
"execution_count": 105,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>-3.0</td>\n <td>-4.0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>-4.0</td>\n <td>5.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice -3.0 -4.0\nBob -4.0 5.0"
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "bibliographic-cherry",
"cell_type": "code",
"source": "grades[grades >= 5].fillna(grades.mean().mean())",
"execution_count": 107,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4.0</td>\n <td>4.0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>4.0</td>\n <td>5.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice 4.0 4.0\nBob 4.0 5.0"
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "signal-boards",
"cell_type": "code",
"source": "grades.where(grades >= 5, -1)",
"execution_count": 109,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>-1</td>\n <td>-1</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>-1</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice -1 -1\nBob -1 5"
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "engaged-laundry",
"cell_type": "code",
"source": "grades.where(grades >= 5, -grades)",
"execution_count": 110,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>-3</td>\n <td>-4</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>-4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice -3 -4\nBob -4 5"
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "novel-television",
"cell_type": "code",
"source": "(pd.DataFrame()\n .reindex_like(grades)\n .fillna(1)\n .where(grades >= 5, -grades))",
"execution_count": 114,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>-3.0</td>\n <td>-4.0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>-4.0</td>\n <td>1.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice -3.0 -4.0\nBob -4.0 1.0"
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "consistent-consent",
"cell_type": "code",
"source": "grades",
"execution_count": 115,
"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>Algebra</th>\n <th>Geometry</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>3</td>\n <td>4</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>4</td>\n <td>5</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Algebra Geometry\nstudent \nAlice 3 4\nBob 4 5"
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "piano-incident",
"cell_type": "code",
"source": "grades.reindex(['Geometry', 'Calculus', 'Algebra'], axis=1, \n fill_value=0)",
"execution_count": 119,
"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>Geometry</th>\n <th>Calculus</th>\n <th>Algebra</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>0</td>\n <td>3</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>5</td>\n <td>0</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Geometry Calculus Algebra\nstudent \nAlice 4 0 3\nBob 5 0 4"
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "classified-accident",
"cell_type": "code",
"source": "grades.reindex(['Geometry', 'Calculus'], axis=1, \n fill_value=0)",
"execution_count": 120,
"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>Geometry</th>\n <th>Calculus</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>0</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>5</td>\n <td>0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Geometry Calculus\nstudent \nAlice 4 0\nBob 5 0"
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "brave-stamp",
"cell_type": "code",
"source": "grades[['Geometry', 'Algebra']]",
"execution_count": 121,
"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>Geometry</th>\n <th>Algebra</th>\n </tr>\n <tr>\n <th>student</th>\n <th></th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>Alice</th>\n <td>4</td>\n <td>3</td>\n </tr>\n <tr>\n <th>Bob</th>\n <td>5</td>\n <td>4</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " Geometry Algebra\nstudent \nAlice 4 3\nBob 5 4"
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "champion-marketplace",
"cell_type": "code",
"source": "grades[['Geometry', 'Algebra', 'Calculus']]",
"execution_count": 122,
"outputs": [
{
"ename": "KeyError",
"evalue": "\"['Calculus'] not in index\"",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/h2/9nyrt4p55kq6pdvqg02_zmj40000gn/T/ipykernel_34981/2605385249.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgrades\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Geometry'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Algebra'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Calculus'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3462\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_iterator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3463\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3464\u001b[0;31m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_listlike_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3465\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3466\u001b[0m \u001b[0;31m# take() does not accept boolean indexers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_get_listlike_indexer\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1312\u001b[0m \u001b[0mkeyarr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnew_indexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_reindex_non_unique\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkeyarr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1313\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1314\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate_read_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkeyarr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1315\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1316\u001b[0m if needs_i8_conversion(ax.dtype) or isinstance(\n",
"\u001b[0;32m~/miniconda3/envs/py3.10/lib/python3.10/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_validate_read_indexer\u001b[0;34m(self, key, indexer, axis)\u001b[0m\n\u001b[1;32m 1375\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1376\u001b[0m \u001b[0mnot_found\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mensure_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmissing_mask\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnonzero\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munique\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1377\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"{not_found} not in index\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1378\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1379\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: \"['Calculus'] not in index\""
]
}
]
},
{
"metadata": {
"trusted": false
},
"id": "recognized-cigarette",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py3.10",
"display_name": "Python 3.10",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.10.0",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Untitled.ipynb",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment