"## Example 6: What are the unique ages for each lecture?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(len(lectures_of_ages)):\n",
" ages_of_lecture_i = ...\n",
" unique_ages = ...\n",
" print(unique_ages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## You try!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"metadata": {},
"source": [
"## Exercise 1: Of all runners, how many are procrastinators? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2: What percentage of 18-year-olds have their major declared as \"Other\"?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 3: Does the oldest basil/spinach-loving Business major prefer cats, dogs, or neither?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% 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)...
- 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/
importcsv
defprocess_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
returnexample_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
foriinrange(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
foriinrange(...):
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
defcell(row_idx,col_name):
col_idx=...# get the index of col_name
val=...# get the value of cs220_data at the specified cell
returnval
```
%% 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
foriinrange(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
foriinrange(len(cs220_data)):
current_age=cell(i,"Age")
ifcurrent_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
defcell(row_idx,col_name):
col_idx=cs220_header.index(col_name)
val=cs220_data[row_idx][col_idx]
if...:
returnNone
elif...:
returnint(val)
else:
returnval
```
%% Cell type:markdown id: tags:
#### Print out every student's sleep habits and major using the `cell` function
%% Cell type:code id: tags:
``` python
foriinrange(len(cs220_data)):
current_age=cell(i,"Age")
ifcurrent_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=[]
foriinrange(len(cs220_data)):
current_lec=...
current_age=...
if...:# TODO: check for missing data
continue
ifcurrent_lec=="LEC001":
students_lec_001.append(current_age)
elifcurrent_lec=="LEC002":
students_lec_002.append(current_age)
elifcurrent_lec=="LEC003":
students_lec_003.append(current_age)
elifcurrent_lec=="LEC004":
students_lec_004.append(current_age)
elifcurrent_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=[
[],
[],
[],
[],
[]
]
foriinrange(len(cs220_data)):
current_lec=int(cell(i,"Lecture")[-1])-1# Will be a number 0 - 4
current_age=cell(i,"Age")
ifcurrent_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
foriinrange(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?