# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

devlopment = pd.read_excel('data/dev_indicators_ssa.xlsx', sheetname=0)
devlopment.head(3)

timerange = [1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
           2008, 2009, 2010, 2011, 2012, 2013, 2013, 2014, 2015, 2016]

dev_copy = devlopment.copy()

# +
features = ['Year','Country Name','Energy use (kg of oil equivalent) per $1,000 GDP (constant 2011 PPP)',
            'Exports of goods and services (% of GDP)', 'Foreign direct investment, net inflows (% of GDP)',
            'GDP (constant 2010 US$)','GDP (current US$)','GDP (current US$)','GDP per capita (constant 2010 US$)',
            'GDP per capita (current US$)','GDP per capita growth (annual %)',
            'GDP per capita, PPP (constant 2011 international $)', 'GDP per capita, PPP (constant 2011 international $)',
            'Goods exports (BoP, current US$)','Goods exports (BoP, current US$)','Oil rents (% of GDP)',
            'Total natural resources rents (% of GDP)','Net ODA received per capita (current US$)',
            'Total natural resources rents (% of GDP)']

dev_copy = dev_copy[features]
# -

np.log(dev_copy['GDP (constant 2010 US$)'].apply(lambda x: x *1))
dev_copy.head(3)

institution_df = dev_copy.copy()
institution_df.head(3)



institution_df.iloc[:, 2:] = institution_df.iloc[:, 2:].applymap(lambda x: '{:.2f}'.format(x))

institution_df.head()

# before dropping duplicates, 
print("before removing duplicates columns: ",len(institution_df.columns))

#droping duplicates columns
institution_df = institution_df.loc[:,~institution_df.columns.duplicated()]
#after dropping duplicates
print("after removing duplicates columns: ",len(institution_df.columns))

p2016 = pd.read_csv('data/final_p2016.csv')
p2016 = p2016.rename(columns={'year':'Year', 'country':'Country Name'})
p2016.head(3)

full_institution = pd.merge(institution_df, p2016, on = ['Year', 'Country Name'])
full_institution.head(3)



# +
#full_institution['Oil rents (% of GDP)'] = full_institution['Oil rents (% of GDP)'].astype(float)
# -

mask = full_institution['Oil rents (% of GDP)'] == 0.00

mask['Oil rents (% of GDP)'] = "Non Oil Exports"

full_institution.head()

full_institution.info()

# we need to convert some columns to float, some of them are objects
full_institution['Energy use (kg of oil equivalent) per $1,000 GDP (constant 2011 PPP)'] = full_institution['Energy use (kg of oil equivalent) per $1,000 GDP (constant 2011 PPP)'].astype(float)
full_institution['Exports of goods and services (% of GDP)'] = full_institution['Exports of goods and services (% of GDP)'].astype(float)
full_institution['Foreign direct investment, net inflows (% of GDP)']= full_institution['Foreign direct investment, net inflows (% of GDP)'].astype(float)
full_institution['GDP (constant 2010 US$)']= full_institution['GDP (constant 2010 US$)'].astype(float)
full_institution['GDP (current US$)'] = full_institution['GDP (current US$)'].astype(float)
full_institution['GDP per capita (constant 2010 US$)'] = full_institution['GDP per capita (constant 2010 US$)'].astype(float)
full_institution['GDP per capita (current US$)'] = full_institution['GDP per capita (current US$)'].astype(float)
full_institution['GDP per capita growth (annual %)'] = full_institution['GDP per capita growth (annual %)'].astype(float)
full_institution['GDP per capita, PPP (constant 2011 international $)'] = full_institution['GDP per capita, PPP (constant 2011 international $)'].astype(float)
full_institution['Goods exports (BoP, current US$)'] = full_institution['Goods exports (BoP, current US$)'].astype(float)

full_institution.head()

ff = full_institution[['Country Name','Exports of goods and services (% of GDP)']].sort_values('Exports of goods and services (% of GDP)', ascending = False)\
.groupby('Country Name')

top_ten_export = ff.first().sort_values('Exports of goods and services (% of GDP)', ascending = False)[:10]
low_ten_export = ff.first().sort_values('Exports of goods and services (% of GDP)', ascending = True)[:10]

top_ten_export

top_ten_export.plot(kind='bar')

low_ten_export

low_ten_export.plot(kind='bar')

institution_df.head()

foreign_investment = full_institution[['Country Name','Foreign direct investment, net inflows (% of GDP)']].sort_values('Foreign direct investment, net inflows (% of GDP)', ascending = False)\
.groupby('Country Name')

top_foreign_investment = foreign_investment.first().sort_values('Foreign direct investment, net inflows (% of GDP)', ascending = False)[:10]
low_foreign_investment = foreign_investment.first().sort_values('Foreign direct investment, net inflows (% of GDP)', ascending = True)[:10]

top_foreign_investment.plot(kind='bar')

low_foreign_investment.plot(kind='bar')

institution_df.head()

gdp_per_country = full_institution[['Country Name','GDP (constant 2010 US$)']].sort_values('GDP (constant 2010 US$)', ascending = False).\
groupby('Country Name')

top_gdp = gdp_per_country.first().sort_values('GDP (constant 2010 US$)', ascending = False)
top_gdp['GDP (constant 2010 US$)'] = top_gdp['GDP (constant 2010 US$)'].apply(lambda x: '{:.2f}'.format(x))

# +
#~top_gdp['Country Name'].duplicated()
#top_gdp = top_gdp[~top_gdp['Country Name'].duplicated()]

# +
#top_gdp

# +
#top_gdp.sort_index()
# -

top_gdp['GDP (constant 2010 US$)'] = top_gdp['GDP (constant 2010 US$)'].astype(float)

#plt.figure(figsize=(120,8))
top_gdp.plot(kind='bar', figsize=(10,8))



StarcoderdataJupyterScriptsDedupFiltered