1 of 13

CSE 163

Section XX

TA1 & TA2

QOTD: If you could meet anyone, living or dead, who would it be and why?

2 of 13

Housekeeping 🏡

Important Dates and Reminders

  • Section Check-In #9 due tomorrow @ 11:59 PM
  • Final Exam is tomorrow, Friday, 8/21 during lecture!!

3 of 13

Game Plan

What we’ll cover today:

  • Practice Problems
  • Open working/office hours/more practice problems?

4 of 13

Course Evaluations!!

Please take 10 minutes to fill out course evaluations for you TAs and professor! :)

5 of 13

Practice Problems

6 of 13

File Processing

Given this starter code, write a function called count_long_words that takes the name of a text file as a parameter and prints the number of words in the file that are longer than 7 characters.

def count_long_words(file_name: str) -> None:

7 of 13

Pandas

Using the DataFrame (df) shown below, compute the average fertility rate in 2017 for all countries whose life expectancy is above 70.

8 of 13

MultiIndex

A MultiIndex using ["country", "year"] has been set of this dataframe. Given this code, select the fertility rate for India in 1967.

df_multi = df.set_index(["country", "year"])

9 of 13

GeoData

Given these data frames and the starter code write a function ‘world_population’ that plots the map of the world with the countries colored by the ratio of the world population that resides within that country. The population of the country is defined as the sum of the populations of the cities in that country while the world population is the sum of the populations of all the countries. Save it as ' world_population . png '

def world_population(df: pd.DataFrame, gdf: gpd.GeoDataFrame) -> None:

merged = gdf.merge(df , left_on ='NAME', right_on ='country’, how='left ')

grouped = merged.dissolve (by='NAME ', aggfunc ='sum ')

10 of 13

Open Office Hours

Please use this time to work on the practice exams and review material!

We’ll be here to answer any and all of your questions :)

11 of 13

Section Code:

12 of 13

Solutions

13 of 13

Solutions

merged = gdf.merge(df, left_on ='NAME', right_on ='country', how='left')

grouped = merged.dissolve (by='NAME', aggfunc ='sum')

total_pop = df['population'].sum()

grouped['ratio'] = grouped['population'] / total_pop

grouped.plot(column ='ratio', legend =True , vmin =0, vmax =1)

plt.savefig('world_population.png')