CSE 163
Section XX
TA1 & TA2
QOTD: If you could meet anyone, living or dead, who would it be and why?
Housekeeping 🏡
Important Dates and Reminders
Game Plan
What we’ll cover today:
Course Evaluations!!
Please take 10 minutes to fill out course evaluations for you TAs and professor! :)
Practice Problems
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:
…
Pandas
Using the DataFrame (df) shown below, compute the average fertility rate in 2017 for all countries whose life expectancy is above 70.
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"])
…
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 ')
…
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 :)
Section Code:
Solutions
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')