text_plain_output_1.png
import os

import pandas as pd

pathvals = ['/kaggle/input/internet-prices-datasets-for-analysis', './data']
fnames = ['average_after_tax_wages.csv', 'average_monthly_internet.csv', 'GDP_per_capita.csv', 'internet_adoption.csv']
average_monthly_internet = 'average_monthly_internet'
average_monthly_internet_costs = 'average_monthly_internet_costs'
average_after_tax_wages = 'average_after_tax_wages'
GDP_per_capita = 'GDP_per_capita'
internet_adoption = 'internet_adoption'
key_names = [average_monthly_internet_costs, average_after_tax_wages, GDP_per_capita, internet_adoption]

df_list = {}
df_list_orig = {}
path = ''
for pathval in pathvals:
    if os.path.exists(pathval):
        path = pathval
        break
for fname in fnames:
    df = pd.read_csv(os.path.join(path, fname))
    key = fname[:-4]
    if key == average_monthly_internet:
        key = average_monthly_internet_costs
    df_list_orig[key] = df
    df_list[key] = df.dropna(axis=1, how='all')
    df_list[key].set_index('Country', inplace=True)
df = df_list[GDP_per_capita]
columns = df.columns
for column in columns:
    df[column] = df[column].str.replace(',', '')
    df[column] = df[column].astype(float)

change_from_13to20 = 'change_from_13to20'
change_from_13to21 = 'change_from_13to21'
change_from_13to20_pct = 'change_from_13to20_pct'
change_from_13to21_pct = 'change_from_13to21_pct'
_2013 = '2013'
_2020 = '2020'
_2021 = '2021'

df_analysis = {}
for key_name in key_names:
    df = df_list[key_name]
    df_new = pd.DataFrame(index=df.index, columns=[change_from_13to20, change_from_13to21, change_from_13to20_pct, change_from_13to21_pct])
    df_new[change_from_13to20] = df[_2020] - df[_2013]
    df_new[change_from_13to20_pct] = (df[_2020] - df[_2013]) / df[_2013] * 100
    df_new[change_from_13to20_pct] = df_new[change_from_13to20_pct].round(decimals=2)
    if _2021 in df.columns:
        df_new[change_from_13to21] = df[_2021] - df[_2013]
        df_new[change_from_13to21_pct] = (df[_2021] - df[_2013]) / df[_2013] * 100
        df_new[change_from_13to21_pct] = df_new[change_from_13to21_pct].round(decimals=2)
    df_analysis[key_name] = df_new

caption_string_neg = '{table}: Negative Changes from {start_year} to {end_year}. Size {shape}. <br> {additional}'
caption_string_pos = '{table}: Positive Changes from {start_year} to {end_year}. Size {shape}. <br> {additional}'
format_dict_nopct = {change_from_13to20: '{:.2f}', change_from_13to21: '{:.2f}'}
format_dict_pct = {change_from_13to20: '{:.2f}', change_from_13to21: '{:.2f}', change_from_13to20_pct: '{:.2f}%', change_from_13to21_pct: '{:.2f}%'}
def display_negative_values(table_name, df=df_analysis, end_year=_2021, format_dict=format_dict_pct, caption_additional=''):
    df_temp = df[table_name]
    if end_year == _2020:
        if change_from_13to20_pct in df_temp:
            df = df_temp[df_temp[change_from_13to20_pct] < -5].sort_values(change_from_13to20_pct, ascending=True)
        else:
            df = df_temp[df_temp[change_from_13to20] < -5].sort_values(change_from_13to20, ascending=True)
    if end_year == _2021:
        if change_from_13to21_pct in df_temp:
            df = df_temp[df_temp[change_from_13to21_pct] < -5].sort_values(change_from_13to21_pct, ascending=True)
        else:
            df = df_temp[df_temp[change_from_13to21] < -5].sort_values(change_from_13to21, ascending=True)
    if format_dict is None:
        return df.style.set_caption(caption_string_neg.format(table=table_name, start_year=_2013, end_year=end_year, shape=df.shape, additional=caption_additional))
    else:
        return df.style.set_caption(caption_string_neg.format(table=table_name, start_year=_2013, end_year=end_year, shape=df.shape, additional=caption_additional)).format(format_dict)
def display_positive_values(table_name, df=df_analysis, end_year=_2021, format_dict=format_dict_pct, caption_additional=''):
    df_temp = df[table_name]
    if end_year == _2020:
        if change_from_13to20_pct in df_temp:
            df = df_temp[df_temp[change_from_13to20_pct] > 5].sort_values(change_from_13to20_pct, ascending=False)
        else:
            df = df_temp[df_temp[change_from_13to20] > 5].sort_values(change_from_13to20, ascending=False)
    if end_year == _2021:
        if change_from_13to21_pct in df_temp:
            df = df_temp[df_temp[change_from_13to21_pct] > 5].sort_values(change_from_13to21_pct, ascending=False)
        else:
            df = df_temp[df_temp[change_from_13to21] > 5].sort_values(change_from_13to21, ascending=False)
    if format_dict is None:
        return df.style.set_caption(caption_string_pos.format(table=table_name, start_year=_2013, end_year=end_year, shape=df.shape, additional=caption_additional))
    else:
        return df.style.set_caption(caption_string_pos.format(table=table_name, start_year=_2013, end_year=end_year, shape=df.shape, additional=caption_additional)).format(format_dict)

display_positive_values(average_monthly_internet_costs)