{ "cells": [ { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "\n", "# Gamma function\n", "from scipy.special import gamma\n", "\n", "# To calculate statistics\n", "from scipy.stats import norm\n", "from scipy.stats import hmean, trim_mean, iqr, median_abs_deviation, skew, kurtosis\n", "from scipy.stats.mstats import gmean, winsorize" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "DataWhr2024 = pd.read_csv(\"../DataWhr2024.csv\")\n", "UnM49 = pd.read_csv(\"./UnM49.csv\", sep=';')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "UnM49 = UnM49[['Country or Area', 'Sub-region Name', 'Region Name']]\n", "UnM49 = UnM49.rename({'Country or Area':'Country name', 'Sub-region Name':'Subregion', 'Region Name':'Continent'}, axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# Data\n", "Dat = pd.merge(DataWhr2024, UnM49)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# Data of 2023\n", "Dat2023 = Dat[Dat['year'] == 2023]\n", "Dat2023 = Dat2023.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Iceland', 'India', 'Indonesia', 'Iraq', 'Ireland', 'Israel', 'Italy']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Countries that starts with the same letter that your name\n", "StartsWith = 'I' # The first letter of your name\n", "list(Dat[Dat['Country name'].str.startswith(StartsWith)]['Country name'].unique())" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# Data of 2023 from the region selected\n", "CountrySelected = 'Iraq' # Change to the country that you selected\n", "SubregionSelected = Dat[Dat['Country name']==CountrySelected]['Subregion'].unique()[0]\n", "\n", "DatSelected = Dat2023[Dat2023['Subregion']==SubregionSelected]\n", "DatSelected = DatSelected.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affect
count121.0121.000000114.000000121.000000120.000000119.000000114.000000114.000000121.000000121.000000
mean2023.05.6014559.5273770.79253765.2833330.7892270.0278510.7224470.6504880.295818
std0.01.1593811.1514620.1319825.5808700.1247320.1629590.1825860.1133650.085531
min2023.01.4460007.1470000.36800052.2000000.228000-0.2680000.1530000.2610000.114000
25%2023.04.6090008.6125000.70300060.7000000.734500-0.0887500.6632500.5790000.231000
50%2023.05.8680009.6700000.83100066.3000000.8070000.0220000.7810000.6610000.289000
75%2023.06.48200010.4962500.89500069.7750000.8770000.1225000.8445000.7380000.358000
max2023.07.69900011.6760000.97900074.6000000.9650000.5900000.9480000.8430000.516000
\n", "
" ], "text/plain": [ " year Life Ladder Log GDP per capita Social support \\\n", "count 121.0 121.000000 114.000000 121.000000 \n", "mean 2023.0 5.601455 9.527377 0.792537 \n", "std 0.0 1.159381 1.151462 0.131982 \n", "min 2023.0 1.446000 7.147000 0.368000 \n", "25% 2023.0 4.609000 8.612500 0.703000 \n", "50% 2023.0 5.868000 9.670000 0.831000 \n", "75% 2023.0 6.482000 10.496250 0.895000 \n", "max 2023.0 7.699000 11.676000 0.979000 \n", "\n", " Healthy life expectancy at birth Freedom to make life choices \\\n", "count 120.000000 119.000000 \n", "mean 65.283333 0.789227 \n", "std 5.580870 0.124732 \n", "min 52.200000 0.228000 \n", "25% 60.700000 0.734500 \n", "50% 66.300000 0.807000 \n", "75% 69.775000 0.877000 \n", "max 74.600000 0.965000 \n", "\n", " Generosity Perceptions of corruption Positive affect Negative affect \n", "count 114.000000 114.000000 121.000000 121.000000 \n", "mean 0.027851 0.722447 0.650488 0.295818 \n", "std 0.162959 0.182586 0.113365 0.085531 \n", "min -0.268000 0.153000 0.261000 0.114000 \n", "25% -0.088750 0.663250 0.579000 0.231000 \n", "50% 0.022000 0.781000 0.661000 0.289000 \n", "75% 0.122500 0.844500 0.738000 0.358000 \n", "max 0.590000 0.948000 0.843000 0.516000 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dat2023.describe()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
0Afghanistan20231.446NaN0.36855.20.228NaN0.7380.2610.460Southern AsiaAsia
1Albania20235.4459.6890.69169.20.8720.0680.8550.5970.314Southern EuropeEurope
2Argentina20236.3939.9940.89267.30.832-0.1290.8460.7200.301Latin America and the CaribbeanAmericas
3Armenia20235.6799.7300.81968.20.819-0.1790.6810.5750.423Western AsiaAsia
4Australia20237.02510.8460.89671.20.8760.1870.4820.7310.248Australia and New ZealandOceania
..........................................
116Uruguay20236.66210.1220.90867.50.904-0.0500.6620.7530.265Latin America and the CaribbeanAmericas
117Uzbekistan20236.3859.0260.90965.90.9270.2470.6500.7520.202Central AsiaAsia
118Yemen20233.532NaN0.82556.60.583NaN0.7710.4470.341Western AsiaAsia
119Zambia20233.6868.1150.66456.10.8540.0920.8140.6530.359Sub-Saharan AfricaAfrica
120Zimbabwe20233.5727.6790.69455.00.735-0.0690.7570.6100.179Sub-Saharan AfricaAfrica
\n", "

121 rows × 13 columns

\n", "
" ], "text/plain": [ " Country name year Life Ladder Log GDP per capita Social support \\\n", "0 Afghanistan 2023 1.446 NaN 0.368 \n", "1 Albania 2023 5.445 9.689 0.691 \n", "2 Argentina 2023 6.393 9.994 0.892 \n", "3 Armenia 2023 5.679 9.730 0.819 \n", "4 Australia 2023 7.025 10.846 0.896 \n", ".. ... ... ... ... ... \n", "116 Uruguay 2023 6.662 10.122 0.908 \n", "117 Uzbekistan 2023 6.385 9.026 0.909 \n", "118 Yemen 2023 3.532 NaN 0.825 \n", "119 Zambia 2023 3.686 8.115 0.664 \n", "120 Zimbabwe 2023 3.572 7.679 0.694 \n", "\n", " Healthy life expectancy at birth Freedom to make life choices \\\n", "0 55.2 0.228 \n", "1 69.2 0.872 \n", "2 67.3 0.832 \n", "3 68.2 0.819 \n", "4 71.2 0.876 \n", ".. ... ... \n", "116 67.5 0.904 \n", "117 65.9 0.927 \n", "118 56.6 0.583 \n", "119 56.1 0.854 \n", "120 55.0 0.735 \n", "\n", " Generosity Perceptions of corruption Positive affect Negative affect \\\n", "0 NaN 0.738 0.261 0.460 \n", "1 0.068 0.855 0.597 0.314 \n", "2 -0.129 0.846 0.720 0.301 \n", "3 -0.179 0.681 0.575 0.423 \n", "4 0.187 0.482 0.731 0.248 \n", ".. ... ... ... ... \n", "116 -0.050 0.662 0.753 0.265 \n", "117 0.247 0.650 0.752 0.202 \n", "118 NaN 0.771 0.447 0.341 \n", "119 0.092 0.814 0.653 0.359 \n", "120 -0.069 0.757 0.610 0.179 \n", "\n", " Subregion Continent \n", "0 Southern Asia Asia \n", "1 Southern Europe Europe \n", "2 Latin America and the Caribbean Americas \n", "3 Western Asia Asia \n", "4 Australia and New Zealand Oceania \n", ".. ... ... \n", "116 Latin America and the Caribbean Americas \n", "117 Central Asia Asia \n", "118 Western Asia Asia \n", "119 Sub-Saharan Africa Africa \n", "120 Sub-Saharan Africa Africa \n", "\n", "[121 rows x 13 columns]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dat2023" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
13Afghanistan20221.281NaN0.22854.8750.368NaN0.7330.2060.576Southern AsiaAsia
\n", "
" ], "text/plain": [ " Country name year Life Ladder Log GDP per capita Social support \\\n", "13 Afghanistan 2022 1.281 NaN 0.228 \n", "\n", " Healthy life expectancy at birth Freedom to make life choices \\\n", "13 54.875 0.368 \n", "\n", " Generosity Perceptions of corruption Positive affect Negative affect \\\n", "13 NaN 0.733 0.206 0.576 \n", "\n", " Subregion Continent \n", "13 Southern Asia Asia " ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dat.loc[Dat[\"Life Ladder\"] == Dat[\"Life Ladder\"].min()]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Country nameyearLife LadderLog GDP per capitaSocial supportHealthy life expectancy at birthFreedom to make life choicesGenerosityPerceptions of corruptionPositive affectNegative affectSubregionContinent
497Denmark20058.01910.8490.97268.30.971NaN0.2370.7770.154Northern EuropeEurope
\n", "
" ], "text/plain": [ " Country name year Life Ladder Log GDP per capita Social support \\\n", "497 Denmark 2005 8.019 10.849 0.972 \n", "\n", " Healthy life expectancy at birth Freedom to make life choices \\\n", "497 68.3 0.971 \n", "\n", " Generosity Perceptions of corruption Positive affect Negative affect \\\n", "497 NaN 0.237 0.777 0.154 \n", "\n", " Subregion Continent \n", "497 Northern Europe Europe " ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dat.loc[Dat[\"Life Ladder\"] == Dat[\"Life Ladder\"].max()]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "avg_health = Dat[\"Healthy life expectancy at birth\"].median()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65.22\n" ] } ], "source": [ "print(avg_health)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(0.231)" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#p = [0.25, 0.5, 0.75]\n", "np.quantile(Dat2023[\"Negative affect\"], 0.25)\n", "\n", "#Dat[\"Negative affect\"]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4.6125, 5.42 , 6.3215])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = [0.25, 0.5, 0.75]\n", "np.quantile(Dat[\"Life Ladder\"], p)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.42\n", "5.4660082404265635\n", "5.3416056329049155\n", "5.207292342092034\n", "5.446561471442401\n", "1.7089999999999996\n", "5.4660082404265635\n" ] } ], "source": [ "ll = Dat[\"Life Ladder\"]\n", "\n", "print(ll.median())\n", "print(ll.mean())\n", "print(gmean(ll))\n", "print(hmean(ll))\n", "print(trim_mean(ll, 0.25))\n", "print(iqr(ll).mean())\n", "print(winsorize(ll).mean())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.6" } }, "nbformat": 4, "nbformat_minor": 2 }