0% found this document useful (0 votes)
33 views10 pages

Code (Misbha & Nishi)

This document imports libraries and loads data on happiness from a CSV file. It then cleans the data, plots relationships between variables like GDP and happiness score, and analyzes metrics like corruption and life expectancy by region. Key findings include that corruption is lowest in Central/Eastern Europe and highest in Sub-Saharan Africa, and that the happiest countries tend to have high freedom and low corruption while the least happy often have lower life expectancy.

Uploaded by

rr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

Code (Misbha & Nishi)

This document imports libraries and loads data on happiness from a CSV file. It then cleans the data, plots relationships between variables like GDP and happiness score, and analyzes metrics like corruption and life expectancy by region. Key findings include that corruption is lowest in Central/Eastern Europe and highest in Sub-Saharan Africa, and that the happiest countries tend to have high freedom and low corruption while the least happy often have lower life expectancy.

Uploaded by

rr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

import numpy as np

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

sns.set_style('darkgrid')
plt.rcParams['font.size'] = 15
plt.rcParams['figure.figsize'] = (10,7)
plt.rcParams['figure.facecolor'] ='#CBC3E3'

data = pd.read_csv('/content/world-happiness-report-2021-x2.csv')

data.head()

OUTPUT :

data = data[data_columns].copy()

happy_df = data.rename({'Country': 'country', 'Region': 'region',


'Happiness_score': 'happiness_score', 'Economy': 'economy', 'Family':
'family', 'Life_Expectancy': 'life_expectancy', 'Freedom': 'freedom',
'Government_Corruption': 'corruption', 'Generosity': 'generosity'})
happy_df.isnull().sum()

OUTPUT:
Country 0
Region 0
Happiness_score 0
Economy 0
Family 0
Life_Expectancy 0
Freedom 0
Government_Corruption 0
Generosity 0
dtype: int64

PLOT BETWEEN HAPPINESS AND GDP

plt.rcParams['figure.figsize'] = (15,7)
plt.title('Plot between Happiness Score and GDP')
sns.scatterplot(x = happy_df.Happiness_score, y = happy_df.Economy, hue =
happy_df.Region, s = 200);
plt.legend(loc = 'upper left', fontsize = '10')
plt.xlabel('Happiness Score')
plt.ylabel('GDP per capita')

OUTPUT:
gdp_region = happy_df.groupby ('Region') ['Economy'].sum()
gdp_region

OUTPUT:

Region
Australia and New Zealand 2.58376
Central and Eastern Europe 27.33071
Eastern Asia 6.91068
Latin America and Caribbean 19.28994
Middle East and Northern Africa 21.33947
North America 2.72080
Southeastern Asia 7.10149
Southern Asia 3.92340
Sub-Saharan Africa 15.21892
Western Europe 27.27051
Name: Economy, dtype: float64

GDP BY REGION

gdp_region.plot.pie(autopct = '%1.1f%%')
plt.title('GDP by Region')
plt.ylabel('')

OUTPUT:
TOTAL COUNTRIES:

total_country = happy_df.groupby('Region')[['Country']].count()
print(total_country)

OUTPUT:
Region Country
Australia and New Zealand 2
Central and Eastern Europe 29
Eastern Asia 6
Latin America and Caribbean 22
Middle East and Northern Africa 20
North America 2
Southeastern Asia 9
Southern Asia 7
Sub-Saharan Africa 40
Western Europe 21

CORRUPTION IN REGIONS

#corruption in regions
corruption = happy_df.groupby('Region')[['Government_Corruption']].mean()
corruption

OUTPUT:
Region Government_Corruption
Australia and New Zealand 0.392795
Central and Eastern Europe 0.086674
Eastern Asia 0.127695
Latin America and Caribbean 0.117172
Middle East and Northern Africa 0.181702
North America 0.244235
Southeastern Asia 0.151276
Southern Asia 0.102536
Sub-Saharan Africa 0.123878
Western Europe 0.231463
CORRUPTION IN VARIOUS REGIONS

plt.rcParams['figure.figsize'] = (12,8)
plt.title('Corruption in various Regions')
plt.xlabel('Regions',fontsize = 15)
plt.ylabel('Corruption Index', fontsize = 15)
plt.xticks (rotation = 30, ha='right')
plt.bar (corruption.index, corruption.Government_Corruption)

OUTPUT:
TOP 10 AND BOTTOM 10 HAPPIEST COUNTRIES LIFE EXPECTANCY

top_10 = happy_df.head(10)
bottom_10 = happy_df.tail(10)

fig, axes= plt.subplots(1,2, figsize= (16, 6))


plt.tight_layout (pad= 2)
xlabels= top_10.Country
axes[0].set_title('Top 10 happiest countries Life Expectancy')
axes[0].set_xticklabels (xlabels, rotation=45, ha='right')
sns.barplot(x= top_10.Country, y= top_10.Life_Expectancy, ax= axes[0])
axes[0].set_xlabel('Country')
axes[0].set_ylabel('Life expectancy')
xlabels= bottom_10.Country
axes[1].set_title('Bottom 10 least happy countries Life Expectancy')
axes[1].set_xticklabels(xlabels, rotation=45, ha='right')
sns.barplot(x= bottom_10.Country, y= bottom_10.Life_Expectancy, ax=
axes[1])
axes[1].set_xlabel('Country Name')
axes[1].set_ylabel('Life expectancy')

OUTPUT:
BETWEEN HAPPINESS SCORE AND FREEDOM TO MAKE LIFE CHOICES

plt.rcParams["figure.figsize"]= (15, 7)
sns.scatterplot(x = happy_df.Freedom, y = happy_df.Happiness_score, hue =
happy_df.Region, s = 200)
plt.legend(loc= 'upper left', fontsize = '12')
plt.xlabel('Freedom to make life choices')
plt.ylabel('Happiness Score')

OUTPUT:
COUNTRIES WITH MOST PERCEPTION OF CORRUPTION

country = happy_df.sort_values(by= 'Government_Corruption').tail(10)


plt.rcParams['figure.figsize'] = (12, 6)
plt.title('Countries with Most Perception of Corruption')
plt.xlabel('Country', fontsize= 13)
plt.ylabel('Corruption Index', fontsize = 13)
plt.xticks(rotation= 30, ha='right')
plt.bar(country.Country, country.Government_Corruption)

OUTPUT:
BETWEEN HAPPINESS SCORE AND CORRUPTION

plt.rcParams["figure.figsize"]= (15, 7)
sns.scatterplot(x = happy_df.Happiness_score, y =
happy_df.Government_Corruption, hue = happy_df.Region, s = 200)
plt.legend(loc= 'lower left', fontsize = '14')
plt.xlabel('Happiness Score')
plt.ylabel('Corruption')

OUTPUT:
CORRUPTION VS HAPPINESS

plt.rcParams['figure.figsize']=(15, 8)
sns.scatterplot(x = happy_df.Happiness_score,
y=happy_df.Government_Corruption, hue = happy_df.Region, s = 200)
plt.legend(loc='lower left', fontsize = '14')
plt.xlabel('Happiness Score')
plt.ylabel('Corruption')

OUTPUT:

You might also like