Skip to content
Snippets Groups Projects
Commit cda9a0f5 authored by msyamkumar's avatar msyamkumar
Browse files

Merge branch 'main' of git.doit.wisc.edu:cdis/cs/courses/cs220/cs220-lecture-material

parents 90906987 95549b7f
No related branches found
No related tags found
No related merge requests found
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
This diff is collapsed.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup #1: Take a look at these list methods # Warmup #1: Take a look at these list methods
# https://www.w3schools.com/python/python_ref_list.asp # https://www.w3schools.com/python/python_ref_list.asp
dairy = ["milk", "ice cream", "cheese", "yogurt" ] dairy = ["milk", "ice cream", "cheese", "yogurt" ]
#use the .index() method to get the index of "ice cream" #use the .index() method to get the index of "ice cream"
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup #2: Because a list is a sequence, we can use the 'in' operator # Warmup #2: Because a list is a sequence, we can use the 'in' operator
food_shelf = ["peanut butter", "milk", "bread", "cheese", "YOGURT"] food_shelf = ["peanut butter", "milk", "bread", "cheese", "YOGURT"]
for item in food_shelf: for item in food_shelf:
if ???: if ... :
print(item, "is dairy") print(item, "is dairy")
else: else:
print(item, "is not dairy") print(item, "is not dairy")
``` ```
%% Cell type:code id: tags: %% Output
``` python
# Warmup #3: finish Monday's lecture problems
```
%% Cell type:code id: tags: peanut butter is dairy
milk is dairy
``` python bread is dairy
# Warmup #4: Open cs220_survey_data.csv from your Desktop cheese is dairy
# Take notes here YOGURT is dairy
# Look at the slides for today
```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# CS220: Lecture 15 # Lecture 15: CSV Files
## Learning Objectives ## Learning Objectives
After this lecture you will be able to... After this lecture you will be able to...
- Open an Excel file and export it to a Comma Separated Value file. - Open an Excel file and export it to a Comma Separated Value file.
- Open a CSV file in TextEditor/Jupyter and connect the elements of the CSV file to the rows and columns in the spreadsheet. - Open a CSV file in TextEditor/Jupyter and connect the elements of the CSV file to the rows and columns in the spreadsheet.
- Use pre-written Python code to read a CSV file into a list of lists. - Use pre-written Python code to read a CSV file into a list of lists.
- Write Python statements with double list indexing to access any element of a CSV file via a list of lists. - Write Python statements with double list indexing to access any element of a CSV file via a list of lists.
- Write code that answers questions about CSV data by writing for loops on lists of lists. - Write code that answers questions about CSV data by writing for loops on lists of lists.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Open the file of student survey data in Jupyter
# Then open it in Windows ... what program opened?
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Open a CSV file in TextEditor/Jupyter and connect the elements of the CSV file to the rows and columns in the spreadsheet. ### Open a CSV file in TextEditor/Jupyter and connect the elements of the CSV file to the rows and columns in the spreadsheet.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# What do you notice? Take notes here # What do you notice? Take notes here
# #
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Use pre-written Python code to read a CSV file into a list of lists. ## Use pre-written Python code to read a CSV file into a list of lists.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# copied from https://automatetheboringstuff.com/chapter14/ # inspired by https://automatetheboringstuff.com/chapter14/
import csv import csv
def process_csv(filename): def process_csv(filename):
# open the file, its a text file utf-8 # open the file, its a text file utf-8
exampleFile = open(filename, encoding="utf-8") exampleFile = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object # prepare it for reading as a CSV object
exampleReader = csv.reader(exampleFile) exampleReader = csv.reader(exampleFile)
# use the built-in list function to convert this into a list of lists # use the built-in list function to convert this into a list of lists
exampleData = list(exampleReader) exampleData = list(exampleReader)
# close the file to tidy up our workspace # close the file to tidy up our workspace
exampleFile.close() exampleFile.close()
# return the list of lists # return the list of lists
return exampleData return exampleData
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Call the process_csv function and store the list of lists in cs220_csv # Call the process_csv function and store the list of lists in cs220_csv
cs220_csv = process_csv('cs220_survey_data.csv') cs220_csv = process_csv('amfam_survey_data.csv')
cs220_csv cs220_csv[:3]
``` ```
%% Output
[['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']]
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Store the header row into cs220_header # Store the header row into cs220_header
cs220_header = cs220_csv[0] cs220_header = cs220_csv[0]
cs220_header cs220_header
``` ```
%% Output
['pizza topping',
'state',
'years',
'sleep preference',
'month',
'pets',
'lat-long']
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Store all of the data rows into cs220_data # Store all of the data rows into cs220_data
cs220_data = cs220_csv[1:] cs220_data = cs220_csv[1:]
cs220_data cs220_data
``` ```
%% Output
[['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']]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## CSVs as a List of Lists ## CSVs as a List of Lists
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Determine how many students completed the survey. # Determine how many students completed the survey.
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# show the entire 1st row of data # show the entire 1st row of actual data
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Find the sleeping habit for the 2nd student...by hardcoding its row and column.... # Find the pizza topping for the 2nd student...by hardcoding its row and column....
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Find the lecture number of the 4th student...by hardcoding its row and column.... # Find the lat-long of the 4th student...by hardcoding its row and column....
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Print out every student's sleep habits and major # Print out every student's sleep habit and pets
# use for # use for
for row in cs220_data: for row in cs220_data:
current_sleep_habit = None current_sleep_habit = None
current_major = None pets = None
print(current_sleep_habit + '\t\t' + current_major) print(current_sleep_habit + '\t\t' + pets)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Create a list containing the average age of LEC003. # Create a list containing the average years of people who do not live in Wisconsin
lecture = "LEC003"
for row in cs220_data: for row in cs220_data:
pass pass
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## It would be nice to have a helper function! ## It would be nice to have a helper function!
Let's introduce `cell` A function that easily accesses a `cell`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# How do we get the names of all the columns? # How do we get the names of all the columns?
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Get the column index of "Pizza topping" # Get the column index of "Pizza topping"
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# We want to invoke something like... # We want to invoke something like...
# cell(24, "Pet owner") # cell(24, <colName>)
# cell(63, "Zip Code")
def cell_v1(row_idx, col_name): def cell_v1(row_idx, col_name):
col_idx = ??? # get the index of col_name col_idx = ??? # get the index of col_name
val = ??? # get the value of cs220_data at the specified cell val = ??? # get the value of cs220_data at the specified cell
return val return val
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Print out the lecture number of the 4th student... using the cell function # Print out the lecture number of the 4th student... using the cell function
cell_v1(???, ???) cell_v1(???, ???)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Redo sleep habits and major using the cell function # Redo above probllem cell function
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# redo average age using the cell function # redo .... using the cell function
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Improve the cell function so it returns the appropriate type. # Improve the cell function so it returns the appropriate type.
# If there is nothing in the cell, return None # If there is nothing in the cell, return None
# otherwise, use the col_name to convert to the expected type # otherwise, use the col_name to convert to the expected type
def cell(row_idx, col_name): def cell(row_idx, col_name):
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# # redo again using the improved cell function # # redo again using the improved cell function
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Practice problems ## Practice problems
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Count how many people are both an Engineer and a pet owner.
```
%% Cell type:code id: tags:
``` python
# What percentage of students chose a non-meat pizza topping? # What percentage of students chose a non-meat pizza topping?
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# What do you want to find out ? # What do you want to find out ?
``` ```
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment