Instantly share code, notes, and snippets.

# ischurov/Lesson15.ipynb Secret

Created March 17, 2022 15:52
Untitled.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 { "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": "
\n\n\n
\n \n \n \n \n \n \n \n
0AliceAlgebra4
1AliceGeometry5
2BobAlgebra3
3BobGeometry5
4ClaudiaAlgebra4
5ClaudiaGeometry3
\n\n\n
courseAlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice4.05.0
Bob3.0NaN
Claudia4.03.0
\n\n\n
courseAlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice45
Bob35
Claudia43
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
studentAlgebraGeometry
\n \n \n \n \n \n \n \n
0Alice34
1Bob45
\n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n
0AliceAlgebra3
1BobAlgebra4
2AliceGeometry4
3BobGeometry5
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
courseAlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice45
Bob35
Claudia43
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
coursestudentAlgebraGeometry
\n \n \n \n \n \n \n \n
0Alice45
1Bob35
2Claudia43
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
courseAlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice45
Bob35
Claudia43
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
courseAlgebraGeometry
\n \n \n \n \n \n \n
045
135
243
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
studentidx
\n \n \n \n \n \n \n
0Winnie4323
1Piglet1242
2Owl123
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n
012425
11533
243234
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0Winnie432343234
1Piglet124212425
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0Winnie43234323.04.0
1Piglet12421242.05.0
2Owl123NaNNaN
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0Piglet1242.012425
1NaNNaN1533
2Winnie4323.043234
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0Winnie4323.04323.04.0
1Piglet1242.01242.05.0
2Owl123.0NaNNaN
3NaNNaN153.03.0
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0Winnie432343234
1Winnie432343233
2Winnie the Pooh432343234
3Winnie the Pooh432343233
4Piglet124212425
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n
012425
11533
243234
343233
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n
0Winnie43234.0
0Winnie43233.0
1Piglet12425.0
2Winnie the Pooh43234.0
2Winnie the Pooh43233.0
3Owl123NaN
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
studentidx
\n \n \n \n \n \n \n
0Winnie4323
1Piglet1242
2Winnie the Pooh4323
3Owl123
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0WinniePooh35
1ChristoferRobin74
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
\n \n \n \n \n \n \n \n \n
0WinniePooh35
1ChristoferRobin74
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
studentAlgebraGeometry
\n \n \n \n \n \n \n \n
0Alice34
1Bob45
\n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice34
Bob45
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Bob45
\n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice-1.0-1.0
Bob-1.05.0
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice34
Bob45
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice3.54.5
Bob3.55.0
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice-3.0-4.0
Bob-4.05.0
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice4.04.0
Bob4.05.0
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice-1-1
Bob-15
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice-3-4
Bob-45
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice-3.0-4.0
Bob-4.01.0
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
AlgebraGeometry
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice34
Bob45
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
GeometryCalculusAlgebra
student
\n \n \n \n \n \n \n \n \n \n \n \n \n \n
Alice403
Bob504
\n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
GeometryCalculus
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice40
Bob50
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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": "
\n\n\n
GeometryAlgebra
student
\n \n \n \n \n \n \n \n \n \n \n \n
Alice43
Bob54
\n \n \n \n \n \n \n \n \n \n \n \n\n", "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\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 }