<jupyter_start><jupyter_text>Pandas Dataframes© Explore Data Science Academy  Instructions to Students- **Do not add or remove cells in this notebook. Do not edit or remove the ` START FUNCTION` or ` END FUNCTION` comments. Do not add any code outside of the functions you are required to edit. Doing any of this will lead to a mark of 0%!**- Answer the questions according to the specifications provided.- Use the given cell in each question to to see if your function matches the expected outputs.- Do not hard-code answers to the questions.- The use of stackoverflow, google, and other online tools are permitted. However, copying fellow student's code is not permissible and is considered a breach of the Honour code below. Doing this will result in a mark of 0%.- Good luck, and may the force be with you!  Honour Code I **YOUR NAME, YOUR SURNAME**, confirm - by submitting this document - that the solutions in this notebook are a result of my own work and that I abide by the   EDSA Student Manifesto.Non-compliance with the honour code constitutes a material breach of contract.  Import the required libraries<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Data You will need these dataframes in order to answer the following questions.<jupyter_code>country_map_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/country_code_map.csv', index_col='Country Code')
population_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/world_population.csv', index_col='Country Code')
meta_df = pd.read_csv('https://raw.githubusercontent.com/Explore-AI/Public-Data/master/AnalyseProject/metadata.csv', index_col='Country Code')<jupyter_output><empty_output><jupyter_text>_**Dataframe specifications:**_The dataframes provide information about the population of the world for various years. Some things to note:* All dataframes have a `Country Code` as an index, which is a three letter code referring to a country.* The `country_map_df` data maps the `Country Code` to a `Country Name`.* The `population_df` data contains information on the population for a given country between the years of 1960 and 2017.* The `meta_df` data contains meta information about each country, including it's geographical region, it's income group, and a comment on the country as a whole.<jupyter_code>country_map_df.head()
population_df.head()
meta_df.head()<jupyter_output><empty_output><jupyter_text>Using this information, answer the questions below:  Question 1 Write a function that returns the summed population total in a given geographic region for a given year._**Function Specifications:**_* Should take as input a year as an `int` and region as a `str`.* Should return an `int` corresponding to the population.<jupyter_code>### START FUNCTION
def total_pop_in_region(year,region):
    # your code here
    counter = 0
    searcher = list(zip(meta_df.index.tolist(), meta_df.Region.values.tolist()))
    for code, place in searcher:
        if place == region:
            counter+=population_df.at[code, str(year)]
            
    return counter

### END FUNCTION
total_pop_in_region(1960,'East Asia & Pacific')<jupyter_output><empty_output><jupyter_text>_**Expected Outputs:**_```pythontotal_pop_in_region(1960,'East Asia & Pacific') == 1029332591.0total_pop_in_region(1970,'South Asia') == 712740919.0```  Question 2 Write a function that returns the global yearly population `Growth`, grouped by the `Income Group` and `Year`._**Function Specifications**_* Should not take any inputs.* The years are currently presented as the heading of each row in the population table. The table will have to be melted to produce the appropriate format. You can use `df.melt` to do this, where the variable name should be `Year` and the value name should be `Growth`.* Should group by the `Year` and `Income Group`.* Should only have one column named `Growth`.* The `Income Group` and the `Year` should be indices.* The `Growth` is calculated by taking the yearly difference and dividing it by the total population for each year, multiplied by 100.* Should return a `DataFrame`.<jupyter_code>### START FUNCTION
def population_difference_by_income():
    # your code here
    searcher = list(zip(meta_df.index.tolist(), meta_df['Income Group'].values.tolist()))
    df_working = population_df.copy()
    df_working.insert(0, 'Income Group', '')
    for code, group in searcher:
        df_working.at[code, 'Income Group'] = group
    df_worked = df_working.melt(id_vars = 'Income Group', var_name = 'Year', value_name = 'Growth' )
    df_hold1 = df_worked.groupby(['Income Group', 'Year']).agg(np.sum).diff()
    df_hold2 = df_worked.groupby(['Income Group', 'Year']).agg(np.sum)
    df_hold1['Growth'] = df_hold1['Growth']/df_hold2['Growth']*100
    return df_hold1

### END FUNCTION
population_difference_by_income().head()<jupyter_output><empty_output><jupyter_text>_**Expected Output:**_```pythonpopulation_difference_by_income().head()```>                         Growth              Income Group      Year                        High income      1960      NaN              1961      1.450978              1962      1.261630              1963      1.235893              1964      1.207633        Question 3 Using the function you just created, write a function that returns the average population _growth_ over all years for a given income group. _**Function Specifications:**_* Should take as input a `str` as the income group.* Should raise a `ValueError` if the input is not a valid income group.* Should return a `float`, rounded to 2 decimal places.<jupyter_code>### START FUNCTION
def ave_growth_by_income(income_group):
    # your code here
    checker = set(meta_df['Income Group'].values.tolist())
    if income_group not in checker:
        raise ValueError('Not a valid income group')
    
    searcher = list(zip(meta_df.index.tolist(), meta_df['Income Group'].values.tolist()))
    df_working = population_df.copy()
    df_working.insert(0, 'Income Group', '')
    
    for code, group in searcher:
        df_working.at[code, 'Income Group'] = group
    df_worked = df_working.melt(id_vars = 'Income Group', var_name = 'Year', value_name = 'Growth' )
    df_hold1 = df_worked.groupby(['Income Group', 'Year']).agg(np.sum).diff()
    df_hold2 = df_worked.groupby(['Income Group', 'Year']).agg(np.sum)
    df_hold1['Growth'] = df_hold1['Growth']/df_hold2['Growth']*100
    
    return round((np.sum(df_hold1.loc[income_group]['Growth'])/len(df_hold1.loc[income_group]['Growth'])), 2)
    
### END FUNCTION
ave_growth_by_income('High income')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09946316731285416, 'fraction_numerical': 0.01804354309573516, 'mean_word_length': 4.4439935064935066, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '656370', 'license': 'MIT', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1900, 'n_words': 700, 'path': 'Pandas Challenge/pd_frames_2.ipynb', 'repo_name': 'k-skaap/portfolio', 'size': '6706'}