Skip to content
GDP Per Capita Versus Life Satisfaction (Linear Regression)
Importing Packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.linear_modelLoading Data
oecd_bli = pd.read_csv("Better_Life_Index.csv", thousands = ',') # OECD Better Life Index 2020
gdp_per_capita = pd.read_csv("WEO_Data.csv", thousands = ',', na_values = 'n/a', encoding = 'latin1') # WEO GDP Per Capita 2020oecd_bli.head()gdp_per_capita.head()Preparing Data
oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value") # Setting Country name as Indexgdp_per_capita.rename(columns={"2020": "GDP per capita"}, inplace=True) # Renaming Columns
gdp_per_capita.set_index("Country", inplace=True) # Setting Country name as Indexfull_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
left_index=True, right_index=True) # Merging tablesfull_country_stats.head()full_country_stats.sort_values(by="GDP per capita", inplace=True) # Sorting by GDP per Capitafull_country_stats.head()country_stats = full_country_stats[["GDP per capita", 'Life satisfaction']] # Choosing GDP per Capita and Life Stisfaction
country_stats.head()