{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note: You can explore the [associated workbook](https://mybinder.org/v2/gh/melaniewalsh/Intro-Cultural-Analytics/master?urlpath=lab/tree/book/02-Python/Workbooks/11.5-Dictionaries-WORKBOOK.ipynb) for this chapter in the cloud.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this lesson, we're going to learn about Python dictionaries by drawing on Anelise Shrout's [Bellevue Almshouse Dataset](https://web.archive.org/web/20191220162702/http://www.nyuirish.net/almshouse/the-almshouse-records/), excerpted below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Preview The Bellevue Almshouse Dataset**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true, "tags": [ "remove-input", "output_scroll" ] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
date_infirst_namelast_nameagediseaseprofessiongenderchildren
01847-04-17MaryGallagher28.0recent emigrantmarriedfChild Alana 10 days
11847-04-08JohnSanin (?)19.0recent emigrantlaborermCatherine 2 mo
21847-04-17AnthonyClark60.0recent emigrantlaborermCharles Riley afed 10 days
31847-04-08LawrenceFeeney32.0recent emigrantlaborermChild
41847-04-13HenryJoyce21.0recent emigrantNaNmChild 1 mo
51847-04-14BridgetHart20.0recent emigrantspinsterfChild
61847-04-14MaryGreen40.0recent emigrantspinsterfAnd child 2 months
71847-04-19DanielLoftus27.0destitutionlaborermNaN
81847-04-10JamesDay35.0recent emigrantlaborermNaN
91847-04-10MargaretFarrell30.0recent emigrantwidowfNaN
101847-04-10BridgetDay30.0recent emigrantmarriedfNaN
111847-04-10AnthonyDay0.5recent emigrantNaNmNaN
121847-04-07JamesCollins22.0recent emigrantlaborermNaN
131847-04-07ThomasCollins21.0recent emigrantlaborermNaN
141847-04-07PatWhalen25.0recent emigrantlaborermNaN
151847-04-17DanDelany10.0typhusNaNmNaN
161847-04-09CatherineO'Harra23.0recent emigrantmarriedfNaN
171847-04-09DamielO'Harra25.0recent emigrantlaborermNaN
181847-04-12MargaretDelaney26.0recent emigrantmarriedfNaN
191847-04-12MichaelDelany3.0recent emigrantNaNmNaN
\n", "
" ], "text/plain": [ " date_in first_name last_name age disease profession gender \\\n", "0 1847-04-17 Mary Gallagher 28.0 recent emigrant married f \n", "1 1847-04-08 John Sanin (?) 19.0 recent emigrant laborer m \n", "2 1847-04-17 Anthony Clark 60.0 recent emigrant laborer m \n", "3 1847-04-08 Lawrence Feeney 32.0 recent emigrant laborer m \n", "4 1847-04-13 Henry Joyce 21.0 recent emigrant NaN m \n", "5 1847-04-14 Bridget Hart 20.0 recent emigrant spinster f \n", "6 1847-04-14 Mary Green 40.0 recent emigrant spinster f \n", "7 1847-04-19 Daniel Loftus 27.0 destitution laborer m \n", "8 1847-04-10 James Day 35.0 recent emigrant laborer m \n", "9 1847-04-10 Margaret Farrell 30.0 recent emigrant widow f \n", "10 1847-04-10 Bridget Day 30.0 recent emigrant married f \n", "11 1847-04-10 Anthony Day 0.5 recent emigrant NaN m \n", "12 1847-04-07 James Collins 22.0 recent emigrant laborer m \n", "13 1847-04-07 Thomas Collins 21.0 recent emigrant laborer m \n", "14 1847-04-07 Pat Whalen 25.0 recent emigrant laborer m \n", "15 1847-04-17 Dan Delany 10.0 typhus NaN m \n", "16 1847-04-09 Catherine O'Harra 23.0 recent emigrant married f \n", "17 1847-04-09 Damiel O'Harra 25.0 recent emigrant laborer m \n", "18 1847-04-12 Margaret Delaney 26.0 recent emigrant married f \n", "19 1847-04-12 Michael Delany 3.0 recent emigrant NaN m \n", "\n", " children \n", "0 Child Alana 10 days \n", "1 Catherine 2 mo \n", "2 Charles Riley afed 10 days \n", "3 Child \n", "4 Child 1 mo \n", "5 Child \n", "6 And child 2 months \n", "7 NaN \n", "8 NaN \n", "9 NaN \n", "10 NaN \n", "11 NaN \n", "12 NaN \n", "13 NaN \n", "14 NaN \n", "15 NaN \n", "16 NaN \n", "17 NaN \n", "18 NaN \n", "19 NaN " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas\n", "pandas.read_csv(\"../data/bellevue_almshouse_modified.csv\").head(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{margin} The Bellevue Almshouse Dataset \n", "The Bellevue Almshouse Dataset includes information about Irish-born immigrants who were admitted to the almshouse in the 1840s. The Bellevue Almshouse was part of New York City's public health system, a place where poor, sick, homeless, and otherwise marginalized people were sent — sometimes voluntarily and sometimes forcibly. This dataset was transcribed from the almshouse's own admissions records by Anelise Shrout.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We're using the [Bellevue Almshouse Dataset](https://web.archive.org/web/20191220162702/http://www.nyuirish.net/almshouse/the-almshouse-records/) to practice dictionaries because we want to think deeply about the consequences of reducing human life to data even at this early stage in our Python journey. This immigration data, as Shrout argues in her essay [\"(Re)Humanizing Data: Digitally Navigating the Bellevue Almshouse,\"](https://crdh.rrchnm.org/essays/v01-10-(re)-humanizing-data/) was \"produced with the express purpose of reducing people to bodies; bodies to easily quantifiable aspects; and assigning value to those aspects which proved that the marginalized people to who they belonged were worth less than their elite counterparts.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we used lists with the Bellevue Almshouse data, it was easier than individually assigning individual variables. We could put multiple names into a single list and multiple ages in a single list.\n", "\n", "By using a Python data collection type called a *dictionary*, we can go even further and group each person's name, age, and profession into a single collection." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Indivudal Variables**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "person1_name = 'Mary Gallagher'\n", "person2_name = 'John Sanin (?)'\n", "person1_age = 18\n", "person2_age = 19" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Lists**" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']\n", "ages = [28, 19, 60, 30]\n", "professions = ['married', 'laborer', 'laborer', 'widow']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary**" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "person1 = {\"name\": \"Mary Gallagher\",\n", " \"age\": 28,\n", " \"profession\": \"married\"}" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(person1)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "person2 = {\"name\": \"John Sanin(?)\",\n", " \"age\": 19,\n", " \"profession\": \"laborer\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Key-Value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A dictionary is made up of \"key\"-\"value\" pairs, which are separated by a colon `:` and separated from other key-value pairs by a comma `,`. A dictionary is always enclosed by curly brackets `{}`. " ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "person1 = {\"name\": \"Mary Gallagher\",\n", " \"age\": 28,\n", " \"profession\": \"married\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can check all the keys in a dictionary by using the `.keys()` method or all the values in a dictionary by using the `.values()` method." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'age', 'profession'])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1.keys()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['Mary Gallagher', 28, 'married'])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access Items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access a value in a dictionary by using square brackets `[]` and its key name (kind of like how we indexed a string or a list)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mary Gallagher'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1[\"name\"]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1[\"age\"]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'married'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1[\"profession\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change Item" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can change a value in a dictionary by re-assigning a new value to a dictionary key." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "person1[\"age\"] = 100" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Mary Gallagher', 'age': 100, 'profession': 'married'}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "person1['profession'] = 'spinster'" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Mary Gallagher', 'age': 100, 'profession': 'spinster'}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nested Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also nest a dictionary inside another dictionary." ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "bellevue_people = {\n", " \"person1\":\n", " {\"name\": \"Mary Gallagher\",\n", " \"age\": 28,\n", " \"profession\": \"married\"},\n", " \"person2\":\n", " {\"name\": \"John Sanin(?)\",\n", " \"age\": 19,\n", " \"profession\": \"laborer\"}\n", " }" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bellevue_people['person1']" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "tags": [ "hide-output" ] }, "outputs": [ { "data": { "text/plain": [ "'Mary Gallagher'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bellevue_people['person1']['name']" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'}" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bellevue_people['person2']" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bellevue_people['person2']['age']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterate Through Dictionary" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "person_1\n", "person_2\n" ] } ], "source": [ "for person in bellevue_people.keys():\n", " print(person)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'}\n", "{'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'}\n" ] } ], "source": [ "for person in bellevue_people.values():\n", " print(person)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mary Gallagher is more than 20 years old. She is 28.\n" ] } ], "source": [ "for person in bellevue_people.values():\n", " if person['age'] > 20:\n", " name = person['name']\n", " age = person['age']\n", " print(f'{name} is more than 20 years old. She is {age}.')" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('person_1', {'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'})\n", "('person_2', {'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'})\n" ] } ], "source": [ "for person in bellevue_people.items():\n", " print(person)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }