Skip to content
Snippets Groups Projects
Commit d3a6145c authored by Ashwin Maran's avatar Ashwin Maran
Browse files

Upload New File

parent 247c30c9
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Warmup 1: Take a look at some list methods
List methods [here](https://www.w3schools.com/python/python_ref_list.asp)...
%% Cell type:code id: tags:
``` python
dairy = ["milk", "ice cream", "cheese", "yogurt", "butter"]
# use the .index() method to get the index of "ice cream"
```
%% Cell type:markdown id: tags:
## Warmup 2: Because a list is a sequence, we can use the `in` operator
%% Cell type:code id: tags:
``` python
food_shelf = ["peanut butter", "milk", "bread", "cheese", "YOGURT"]
for item in food_shelf:
if ...:
print(item, "is dairy")
else:
print(item, "is not dairy")
```
%% Cell type:markdown id: tags:
# CS220: Lecture 15
## Learning Objectives
After this lecture you will be able to...
- 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.
- 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 code that answers questions about CSV data by writing for loops on lists of lists.
%% Cell type:markdown id: tags:
## Reading a CSV
%% Cell type:markdown id: tags:
## Example 1: Store the contents of the CSV file into Python lists
%% Cell type:code id: tags:
``` python
# adapted from https://automatetheboringstuff.com/chapter14/
import csv
def process_csv(filename):
# open the file, its a text file utf-8
example_file = open(filename, encoding="utf-8")
# prepare it for reading as a CSV object
example_reader = csv.reader(example_file)
# use the built-in list function to convert this into a list of lists
example_data = list(example_reader)
# close the file to tidy up our workspace
example_file.close()
# return the list of lists
return example_data
```
%% Cell type:markdown id: tags:
#### Call the `process_csv` function and store the list of lists in `cs220_csv`
%% Cell type:code id: tags:
``` python
cs220_csv = process_csv('cs220_survey_data.csv')
cs220_csv
```
%% Cell type:markdown id: tags:
#### Store the header row into `cs220_header`
%% Cell type:code id: tags:
``` python
cs220_header = cs220_csv[0]
cs220_header
```
%% Cell type:markdown id: tags:
#### Store all of the data rows into `cs220_data`
%% Cell type:code id: tags:
``` python
cs220_data = cs220_csv[1:]
cs220_data
```
%% Cell type:markdown id: tags:
## Example 2: CSVs as a List of Lists
%% Cell type:markdown id: tags:
#### Print out the lecture number of the 4th student by hardcoding its row and column
%% Cell type:code id: tags:
``` python
cs220_data[...][...] # [row][col]
```
%% Cell type:markdown id: tags:
#### Print out the sleeping habit for the 2nd student by hardcoding its row and column
%% Cell type:code id: tags:
``` python
cs220_data[...][...]
```
%% Cell type:markdown id: tags:
#### Print out how many students completed the survey
%% Cell type:code id: tags:
``` python
len(...)
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_sleep_habit = ...
current_major = ...
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:markdown id: tags:
#### Print out every students' age in 10 years
Fix the bug in the code below
%% Cell type:code id: tags:
``` python
for i in range(...):
current_age = cs220_data[i][2]
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## It would be nice to have a helper function!
Let's introduce `cell`
%% Cell type:markdown id: tags:
#### Remember creating `cs220_header`?
%% Cell type:code id: tags:
``` python
cs220_header
```
%% Cell type:markdown id: tags:
#### Get the column index of `"Pizza topping"`
%% Cell type:code id: tags:
``` python
cs220_header.index(...)
```
%% Cell type:markdown id: tags:
## Example 3: Create a `cell` function
We want to invoke something like...
* `cell(24, "Pet owner")`
* `cell(63, "Zip Code")`
%% Cell type:code id: tags:
``` python
def cell(row_idx, col_name):
col_idx = ... # get the index of col_name
val = ... # get the value of cs220_data at the specified cell
return val
```
%% Cell type:markdown id: tags:
#### Print out the lecture number of the 4th student using the `cell` function
%% Cell type:code id: tags:
``` python
cell(..., ...)
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major using the `cell` function
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_sleep_habit = ...
current_major = ...
print(current_sleep_habit + '\t\t' + current_major)
```
%% Cell type:markdown id: tags:
#### Print out every students' age in 10 years using the `cell` function
This does not quite work. Fix this code:
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
if current_age != None:
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## Example 4: Improve the `cell` function
Improve the `cell` function so it returns the appropriate type. If there is **nothing** in the cell, return `None`.
%% Cell type:code id: tags:
``` python
def cell(row_idx, col_name):
col_idx = cs220_header.index(col_name)
val = cs220_data[row_idx][col_idx]
if ...:
return None
elif ...:
return int(val)
else:
return val
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major using the `cell` function
%% Cell type:code id: tags:
``` python
for i in range(len(cs220_data)):
current_age = cell(i, "Age")
if current_age != None:
print(current_age + 10)
```
%% Cell type:markdown id: tags:
## Example 5: Get the average age of each lecture
%% Cell type:code id: tags:
``` python
students_lec_001 = []
students_lec_002 = []
students_lec_003 = []
students_lec_004 = []
students_lec_005 = []
for i in range(len(cs220_data)):
current_lec = ...
current_age = ...
if ...: # TODO: check for missing data
continue
if current_lec == "LEC001":
students_lec_001.append(current_age)
elif current_lec == "LEC002":
students_lec_002.append(current_age)
elif current_lec == "LEC003":
students_lec_003.append(current_age)
elif current_lec == "LEC004":
students_lec_004.append(current_age)
elif current_lec == "LEC005":
students_lec_005.append(current_age)
print("Average age for LEC001 is", round(sum(students_lec_001) / len(students_lec_001), 2))
print("Average age for LEC002 is", round(sum(students_lec_002) / len(students_lec_002), 2))
print("Average age for LEC003 is", round(sum(students_lec_003) / len(students_lec_003), 2))
print("Average age for LEC004 is", round(sum(students_lec_004) / len(students_lec_004), 2))
print("Average age for LEC005 is", round(sum(students_lec_005) / len(students_lec_005), 2))
```
%% Cell type:markdown id: tags:
#### Bonus challenge: Can you do this with a little less hardcoding?
%% Cell type:code id: tags:
``` python
lectures_of_ages = [
[],
[],
[],
[],
[]
]
for i in range(len(cs220_data)):
current_lec = int(cell(i, "Lecture")[-1]) - 1 # Will be a number 0 - 4
current_age = cell(i, "Age")
if current_age != None:
lectures_of_ages[current_lec].append(current_age)
# TODO: Print the average ages
```
%% Cell type:markdown id: tags:
## Example 6: What are the unique ages for each lecture?
%% Cell type:code id: tags:
``` python
for i in range(len(lectures_of_ages)):
ages_of_lecture_i = ...
unique_ages = ...
print(unique_ages)
```
%% Cell type:markdown id: tags:
## You try!
%% Cell type:markdown id: tags:
Complete the challenges below. First try completing the problem directly using the list of lists (e.g. double indexing `[][]`), then try using the `cell` function!
%% Cell type:markdown id: tags:
## Exercise 1: Of all runners, how many are procrastinators?
%% Cell type:markdown id: tags:
## Exercise 2: What percentage of 18-year-olds have their major declared as "Other"?
%% Cell type:markdown id: tags:
## Exercise 3: Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither?
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