Question: import pandas as pd import matplotlib.pyplot as plt import numpy as np import helper import sim _ parameters def calculate _ number _ of _

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import helper
import sim_parameters
def calculate_number_of_samples(df, countries, sample_ratio):
'''
function that returns a nested dictionary with the structure: 'Afghanistan' : {less_5 : 3}
'''
population ={} # dictionary, storing each country with its population
for country in countries:
# filter the dataframe to get the population of the current country
country_row = df[df['country']== country]
population[country]= country_row['population'].values[0] # now each country has it's population
num_samples_country =[] # list storing (order of the countries in population) the number of samples of each country
# calculate the number of samples for each country
for country in population:
num_samples_country.append(int((population[country]/sample_ratio)//1)) # floor value using //1 and casting into integer
# harcoded for speeding up process, TODO eliminate this line
num_samples_country =[4,1,12]
print("
Number of samples of each country we are going to use for the moment (faster execution):
", num_samples_country)
# Calculate the number of samples of each age group inside each country
# nested dictionary key: country, value: dictionary with each age group the number of samples
num_group_samples_inside_country ={} # eg: 'Afghanistan' : {less_5 : 3}
# TODO Calculate the values of the dictionary
# -------------
return num_group_samples_inside_country
Please complete the TODO section of this code.
import pandas as pd import matplotlib.pyplot as

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!