# Load a useful Python libraries for handling data
import pandas as pd
from IPython.display import Markdown, display

# Read the data
data_filename = r'gapminder.csv'
data = pd.read_csv(data_filename, low_memory=False)
data = data.set_index('country')

display(Markdown("General information on the Gapminder data"))
display(Markdown("Number of countries: {0}".format(len(data))))
display(Markdown("Number of variables: {0}".format(len(data.columns))))

display(Markdown("The first records of the data."))
data.head()

# Convert interesting variables in numeric format
for variable in ('internetuserate', 'suicideper100th', 'employrate'):
    data[variable] = pd.to_numeric(data[variable], errors='coerce')

"""
Explanation: Writing your first program - Python
Following is the Python program I wrote to fulfill the second assignment of the Data Management and Visualization online course.
I decided to use Jupyter Notebook as it is a pretty way to write code and present results.
Research question
Using the Gapminder database, I would like to see if an increasing Internet usage results in an increasing suicide rate. A study shows that other factors like unemployment could have a great impact.
So for this second assignment, the frequencies of the three following variables will be analyzed:
- Internet Usage Rate (per 100 people)
- Suicide Rate (per 100 000 people)
- Employment Rate (% of the population of age 15+)
Data analysis
End of explanation
"""


data['internetuserate'].value_counts(sort=False, dropna=False)

"""
Explanation: We will now have a look at the frequencies of the variables.
Internet use rate frequencies
First for the Internet usage, the counts are (including data missing):
End of explanation
"""


import numpy as np
display(Markdown("Internet Use Rate (min, max) = ({0:.2f}, {1:.2f})".format(data['internetuserate'].min(), data['internetuserate'].max())))

internetuserate_bins = pd.cut(data['internetuserate'], bins=np.linspace(0, 100., num=21))

"""
Explanation: This is useless as the variable does not take discrete values. So before researching the frequency count on the data, I will group the data in intervals of 5% using the cut function.
End of explanation
"""


internetuserate_bins.value_counts(sort=False, dropna=False)

"""
Explanation: Counts of Internet Use Rate:
End of explanation
"""


internetuserate_bins.value_counts(sort=False, normalize=True, dropna=False)

display(Markdown("Cumulative sum for Internet use rate percentages"))
internetuserate_bins.value_counts(sort=False, normalize=True, dropna=False).cumsum()

"""
Explanation: Percentages of Internet Use Rate:
End of explanation
"""


display(Markdown("Suicide rate (min, max) = ({0:.2f}, {1:.2f})".format(data['suicideper100th'].min(), data['suicideper100th'].max())))

"""
Explanation: Suicide rate per 100,000 frequencies
End of explanation
"""


suiciderate_bins = pd.cut(data['suicideper100th'], bins=np.linspace(0, 40., num=21))
suiciderate_bins.value_counts(sort=False, dropna=False)

"""
Explanation: Counts of Suicide Rate:
End of explanation
"""


suiciderate_bins.value_counts(sort=False, normalize=True, dropna=False)

"""
Explanation: Percentages of Suicide Rate:
End of explanation
"""


display(Markdown("Employment rate (min, max) = ({0:.2f}, {1:.2f})".format(data['employrate'].min(), data['employrate'].max())))

"""
Explanation: Employment rate frequencies
End of explanation
"""


employment_bins = pd.cut(data['employrate'], bins=np.linspace(0, 100., num=21))
employment_bins.value_counts(sort=False, dropna=False)

"""
Explanation: Counts of Employment Rate:
End of explanation
"""


employment_bins.value_counts(sort=False, normalize=True, dropna=False)

"""
Explanation: Percentages of Employment Rate:
End of explanation
"""


high_employment_set = data[data['employrate'] > 60.0]
high_employment_set.head()

"""
Explanation: Summary
The Gapminder data based provides information for 213 countries. Unfortunately for all three variables analyzed here (Internet Use Rate, Suicide rate and employment rate) data are missing; e.g. the internet use rate is unknown for 21 countries.
The distribution of the variables is as follow:
- Internet Use Rate per 100 people
    * Data missing for 21 countries
    * Rate ranges from 0.21 to 95.64
    * The majority of the countries have a rate below 50
- Suicide Rate per 100 000
    * Data missing for 22 countries
    * Rate ranges from 0.2 to 35.75
    * The rate is more often between 4 and 12
- Employment Rate for age 15+
    * Data missing for 35 countries
    * Rate ranges from 32 to 83.2
    * For most of the countries the rate lies between 50 and 65
A possible refinement could to analyze only the country with an high employment rate (like > 60%) to suppress the economical influence.
The data filtering would then be done as follow:
End of explanation
"""


