diff --git a/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/amfam_survey_data.csv b/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/amfam_survey_data.csv new file mode 100644 index 0000000000000000000000000000000000000000..c223c886d305b1d9d536851bfe389576d22f9212 --- /dev/null +++ b/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/amfam_survey_data.csv @@ -0,0 +1,33 @@ +pizza topping,state,years,sleep preference,month,pets,lat-long +mushroom,Florida,7,early bird,March,,"30.263214888389417, -81.54792098150529" +pineapple,Wisconsin,4,night owl,April,other,"43.1581437, -89.2921125" +sausage,Wisconsin,10,early bird,July,other,"43.15645, -89.28814" +pepperoni,WI,7,no preference,September,"dog,cat","43.073051, -89.401230" +mushroom,madison,7,early bird,November,, +pepperoni,FL,1,no preference,December,dog,"42.35623761108948, -71.05691488946681" +pepperoni,Wisconsin,2,night owl,February,,"43.159045128642774, -89.29146323507756" +mushroom,Florida,0.5,night owl,May,other,"43.160601, -89.287671" +mushroom,Wisconsin,10,no preference,January,"dog,fish","43.1562216,-89.2880086" +pineapple,Wisconsin,8,night owl,July,dog,"43.158655, -89.289895" +sausage,Minnesota,15,no preference,August,"dog,cat","45.13881645889933, -93.47636590830673" +pepperoni,New Jersey,1,night owl,May,other,"43.07148896663423, -89.40567798752735" +basil,Rhode Island,1,night owl,March,dog,"43.156490793353775, -89.28796434617352" +mushroom,TX,1,no preference,January,dog, +pineapple,Florida,3,early bird,July,other,"27.979191147972834, -82.33356380365498" +sausage,Wisconsin,0,early bird,December,"dog,cat","43.15631441766965, -89.28785659081201" +pineapple,Wisconsin,6,no preference,June,dog,"43.157716440341964, -89.28939262164963" +mushroom,Florida,7,no preference,July,other,"30.053546, -81.514610" +sausage,Florida,3,early bird,January,"dog,fish","30.263357, -81.547884" +mac&cheese,Wisconsin,5,night owl,July,dog,"43.158328032172754, -89.28946714938327" +pepperoni,Wisconsin,10,early bird,April,other,"43.1884213,-89.2762121" +other,Wisconsin,10,early bird,August,other,"43.15833, -89.28988" +sausage,WI,14,night owl,September,"dog,cat","43.15733597381252, -89.29013010509833" +sausage,Wisconsin,6,no preference,August,"dog,cat","43.159061371631616, -89.29141118826759" +pepperoni,Wisconsin,8,early bird,September,"dog,cat,fish",43.158359 -89.289972 +pineapple,Florida,8,night owl,October,,"30.263432655702932, -81.54807118535949" +pineapple,TX,4,night owl,October,dog,"42.3558293029345, -71.05683171712127" +other,WI,2,early bird,June,, +mushroom,Wisconsin,20,early bird,September,dog,"43.15826500058843, -89.28945716165009" +sausage,Wisconsin,8,night owl,June,dog,"43.15839022178169, -89.28998287477457" +sausage,Wisconsin,20,night owl,April,bird,"43.15648555750267, -89.28783647996661" +pineapple,Texas,0.5,early bird,August,other,"43, 89" \ No newline at end of file diff --git a/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/lec17_dictionaries1_template.ipynb b/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/lec17_dictionaries1_template.ipynb index 4f155350c33f8d7424cc8491684cb1c8d351aa86..676a6398f32b5b60b191a6327b9889ce305dcbbc 100644 --- a/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/lec17_dictionaries1_template.ipynb +++ b/f22/andy_lec_notes/lec17_Oct17_Dictionaries1/lec17_dictionaries1_template.ipynb @@ -18,22 +18,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "721" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "# Warmup 1: Read in the file 'cs220_survey_data.csv' into a lists of lists\n", + "# Warmup 1: Read in the file 'afmfam_survey_data.csv' into a lists of lists\n", "\n", "\n", "# source: Automate the Boring Stuff with Python Ch 12\n", @@ -51,29 +40,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Lecture',\n", - " 'Age',\n", - " 'Primary major',\n", - " 'Other majors',\n", - " 'Zip Code',\n", - " 'Pizza topping',\n", - " 'Pet owner',\n", - " 'Runner',\n", - " 'Sleep habit',\n", - " 'Procrastinator']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Warmup 2: store the first row in a variable called header\n", "header = None\n", @@ -82,53 +51,27 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "[['LEC001',\n", - " '19',\n", - " 'Business: Other',\n", - " '',\n", - " '53706',\n", - " 'pepperoni',\n", - " 'Yes',\n", - " 'No',\n", - " 'early bird',\n", - " 'Yes'],\n", - " ['LEC001',\n", - " '21',\n", - " 'Other',\n", - " 'Economics/Philosophy, Data Science Certificate',\n", - " '53703',\n", - " 'pepperoni',\n", - " 'Yes',\n", - " 'No',\n", - " 'no preference',\n", - " 'Yes'],\n", - " ['LEC003',\n", - " '19',\n", - " 'Computer Science',\n", - " 'Data science',\n", - " '53706',\n", - " 'pineapple',\n", - " 'Yes',\n", - " 'Yes',\n", - " 'night owl',\n", - " 'Yes']]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" + "ename": "TypeError", + "evalue": "'NoneType' object is not subscriptable", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Warmup 3: store the rest of the data in a variable called rows\u001b[39;00m\n\u001b[1;32m 2\u001b[0m rows \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mrows\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: 'NoneType' object is not subscriptable" + ] } ], "source": [ "# Warmup 3: store the rest of the data in a variable called rows\n", "rows = None\n", - "rows\n", + "\n", + "# show the first 3 rows\n", + "\n", "\n" ] }, @@ -151,11 +94,10 @@ "\n", "def count_col_frequency(value, col_name):\n", " ''' returns the frequency of value in col_name '''\n", - " count = 0\n", - " for row in rows:\n", - " if row[header.index(col_name)] == value:\n", - " count += 1\n", - " return count\n" + " \n", + " #Hint: use if row[header.index(col_name)] == value:\n", + " return None\n", + "\n" ] }, { @@ -207,7 +149,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## March 7: Dictionaries\n", + "## Lecture 17: Dictionaries\n", "\n", "Learning Objectives:\n", "- Use correct dictionary syntax \n", @@ -305,10 +247,10 @@ "print(price_dict) # number of key/value pairs\n", "\n", "#get the price for a certain item\n", - "print(price_dict) # name of dict [ key]\n", + "print(...) # name of dict [ key]\n", "\n", "#get the price for donut\n", - "print(price_dict)\n", + "print(...)\n", "\n", "# what's wrong with this line? \n", "# print(price_dict[1.25])\n" @@ -387,6 +329,13 @@ "price_dict" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The .pop() method removes a key/value pair" + ] + }, { "cell_type": "code", "execution_count": 21, @@ -407,6 +356,13 @@ " price_dict.pop('pizza')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The .keys() and .values() methods return a list-like object" + ] + }, { "cell_type": "code", "execution_count": 23, @@ -636,7 +592,7 @@ } ], "source": [ - "# Example 3a. Same as 2a, but use the survey_data\n", + "# Example 3a. Same as 2a, but use the survey_data for pizza topping\n", "major_freq = dict() # another way to make a dictionary\n", "\n", "major_freq" @@ -651,24 +607,28 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 6, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "the major Engineering appeared 328 times\n" + "ename": "NameError", + "evalue": "name 'most_used_key' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Example 3b: use the algorithm from 2b to find the pizza topping with the highest frequency\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# find the frequency of each major\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe pizza topping \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m appeared \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m times\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\u001b[38;5;28mstr\u001b[39m(\u001b[43mmost_used_key\u001b[49m), max_value))\n", + "\u001b[0;31mNameError\u001b[0m: name 'most_used_key' is not defined" ] } ], "source": [ - "# Example 3b: use the algorithm from 2b to find the major with the highest frequency\n", + "# Example 3b: use the algorithm from 2b to find the pizza topping with the highest frequency\n", "\n", "# find the frequency of each major\n", "\n", "\n", - "print(\"the major {} appeared {} times\".format(str(most_used_key), max_value))\n" + "print(\"the pizza topping {} appeared {} times\".format(str(most_used_key), max_value))\n" ] }, { @@ -735,7 +695,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -749,7 +709,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.9.12" } }, "nbformat": 4,