<jupyter_start><jupyter_text>Importing libraries and data<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
GDP2020 = pd.read_excel('GDPCAP.xlsx')
GDPCAP = pd.read_excel ('GDP2020.xlsx')
Bnaires = pd.read_excel('2022_forbes_billionaires.xlsx')<jupyter_output><empty_output><jupyter_text>Cleaning the dataCleaning GDP2020 Per Capita data 1. Overview2. Drop columns .drop("column_name", axis = 1, inplace = True)3. Renaming columns by creating a new list and assigning to the columns4. Dropping rows where there's no information on GDP dropna( subset = ["column_name"], inplace = True )<jupyter_code>GDPCAP.head()
GDPCAP.drop("Country Code", axis = 1 , inplace = True)
columns_rename = ['CountryName','2020GDP']
GDPCAP.columns = columns_rename 
GDPCAP.dropna(subset = ['2020GDP'], inplace = True )<jupyter_output><empty_output><jupyter_text>GDP 2020 table clean1. removing null rows for GDP 2. reformatting GDP<jupyter_code>GDP2020.rename(columns = {"GDPPERCAP" : "GDP" }, inplace = True) 
GDP2020.dropna(subset = ["GDP"], inplace = True )
pd.options.display.float_format = '{:.2f}'.format
GDP2020.nlargest(20,'GDP')<jupyter_output><empty_output><jupyter_text>Cleaning Billionare table 1. dropping unneccessary table 2. concatenating networth to convert to int<jupyter_code>Bnaires.drop("Column1", axis = 1 , inplace = True)
Bnaires["networth"] = Bnaires["networth"].str.replace("B","")
Bnaires["networth"] = Bnaires["networth"].str.replace("$","")
Bnaires["networth"] = Bnaires["networth"].astype(float)



Bnaires.head(50)<jupyter_output><empty_output><jupyter_text>In Billionaires record, some of the countries do not match the names listed in GDP dataFinding unmatched country names from Billionaire data to map it on GDP data1. saving unique country names from each data frame as lists2. using for loop, append country names (from Billionaire list) that do not exist in GDP data3. check the data quality & confirm what needs to be fixed for a good quality mapping4. after mapping recheck the data for both GDP & GDP Per capita country names<jupyter_code>Bnairecountry = Bnaires["country"].unique()
GDPcountry = GDP2020["Country"].unique()
Missingcountry = []
for i in Bnairecountry:
    if i not in GDPcountry: 
        Missingcountry.append(i)
    
Missingcountry
GDP2020.loc[GDP2020["Country"].str.contains("|".join(Missingcountry))]
Bnaires.loc[Bnaires['country'].str.contains('Hong Kong'),'country'] = 'Hong Kong SAR, China'
Bnaires.loc[Bnaires['country'].str.contains('Russia'),'country'] = 'Russian Federation'
Bnaires.loc[Bnaires['country'].str.contains('Czechia'),'country'] = 'Czech Republic'
Bnaires.loc[Bnaires['country'].str.contains('South Korea'),'country'] = 'Korea, Rep.'
Bnaires.loc[Bnaires['country'].str.contains('Slovakia'),'country'] = 'Slovak Republic'
Bnaires.loc[Bnaires['country'].str.contains('Venezuela'),'country'] = 'Venezuela, RB'
Bnaires.loc[Bnaires['country'].str.contains('Guernsey'),'country'] = 'United Kingdom'
Bnaires.loc[Bnaires['country'].str.contains('Macau'),'country'] = 'Macao SAR, China'
Bnaires.loc[Bnaires['country'].str.contains('Taiwan'),'country'] = 'China'
Bnaires.loc[Bnaires['country'].str.contains('Egypt'),'country'] = 'Egypt, Arab Rep.'

Bnairecountry = Bnaires["country"].unique()

maptest = []
for i in Bnairecountry:
    if i not in GDPcountry: 
        maptest.append(i)
    
maptest
Bnaires.loc[Bnaires['country'].str.contains('Eswatini'), "country"] = 'Eswatini'
maptest2 = []

for i in Bnaires['country'].unique():
    if i not in   GDPCAP['CountryName'].unique():
        maptest2.append(i)
maptest2
GDPCAP.loc[GDPCAP['CountryName'].str.contains("Liechten")].sort_values(by = 'CountryName')

GDPCAP.loc[GDPCAP['CountryName'].str.contains("V")].sort_values(by = 'CountryName')<jupyter_output><empty_output><jupyter_text>Exploring and Visualizing data<jupyter_code>Bnaires.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 2600 entries, 0 to 2599
Data columns (total 7 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   rank      2600 non-null   int64  
 1   name      2600 non-null   object 
 2   networth  2600 non-null   float64
 3   age       2600 non-null   int64  
 4   country   2600 non-null   object 
 5   source    2600 non-null   object 
 6   industry  2600 non-null   object 
dtypes: float64(1), int64(2), object(4)
memory usage: 142.3+ KB<jupyter_text>1. Top 10 country with most billionaires<jupyter_code>bnairecountry = Bnaires.groupby("country")
bnairecountry.size().nlargest(10)<jupyter_output><empty_output><jupyter_text>2. Top 10 country with most money from billionaires<jupyter_code>bnairecountry.sum().sort_values("networth", ascending = False)<jupyter_output><empty_output><jupyter_text>3. Average age of billionaire per country , select only top 10<jupyter_code>agedistribution = bnairecountry["age"].describe()
agedistribution["count"] = agedistribution["count"].astype(int)
countfilter = agedistribution.loc[agedistribution["count"]>15]

countfilter[["mean","count"]].nsmallest(10,"mean")<jupyter_output><empty_output><jupyter_text>Using lambda funtion to find out how many billionaires exist in U.S.<jupyter_code>bnairecountries = Bnaires["country"]
len(list(filter(lambda x : "United States" in x, bnairecountries)))<jupyter_output><empty_output><jupyter_text>Joining GDP Per capita and GDP for further analysis and visuzalization<jupyter_code>total_capita = GDP2020.merge(GDPCAP,how = 'inner', left_on= "Country", right_on="CountryName")
total_capita.rename(columns = {"2020GDP":"CAP"}, inplace = True)
total_capita
total_capita["GDP_Rank"] = total_capita["GDP"].rank(ascending = False)
total_capita["CAP_Rank"] = total_capita["CAP"].rank(ascending = False)

total_capita["TotalGDP_standardliving_ratio"] = total_capita["GDP_Rank"]/total_capita["CAP_Rank"]

total_capita.sort_values(by = "TotalGDP_standardliving_ratio", ascending = True)
plt.figure(figsize =(10,6))
sns.regplot(data =total_capita, y = "GDP_Rank", x = "CAP_Rank", color = 'c').set(title = "Capita_Rank vs Total GDP rank")
top20GDP =total_capita.nlargest(20,["GDP"])
top80GDP =total_capita.nlargest(80,["GDP"])
top200GDP = total_capita.nlargest(200,["GDP"])
plt.figure(figsize =(10,6))
sns.regplot(data =top20GDP, y = "GDP_Rank", x = "CAP_Rank", color = 'c').set(title = "Top20 Capita_Rank vs Total GDP rank")
sns.jointplot(data =top20GDP, y = "GDP_Rank", x = "CAP_Rank",kind ='reg')
plt.figure(figsize =(10,6))
sns.regplot(data =top200GDP, y = "GDP_Rank", x = "CAP_Rank", color = 'c').set(title = "Top20 Capita_Rank vs Total GDP rank")

sns.lmplot(data =top20GDP, y= "GDP_Rank", x = "CAP_Rank", hue= 'CountryName',palette= 'Spectral').set(title = "Top20 Capita_Rank vs Total GDP rank")

def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x']+.02, point['y'], str(point['val']))

label_point(top20GDP["CAP_Rank"], top20GDP["GDP_Rank"], top20GDP["CountryName"], plt.gca()) 
plt.gcf().set_size_inches(15, 8)

sns.lmplot(data =top80GDP, y= "GDP_Rank", x = "CAP_Rank", hue= 'CountryName',palette= 'viridis').set(title = "Top80 Capita_Rank vs Total GDP rank")

def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x']+.02, point['y'], str(point['val']))

label_point(top80GDP["CAP_Rank"], top80GDP["GDP_Rank"], top80GDP["CountryName"], plt.gca()) 
plt.gcf().set_size_inches(15, 8)
sns.displot(total_capita['CAP'], bins = 10)<jupyter_output><empty_output><jupyter_text>Sampling data for plots<jupyter_code>sampling = total_capita.sample(int(0.2*len(total_capita)))
                               
plt.figure(figsize =(10,6))
sns.regplot(data =sampling, y = "GDP", x = "CAP", color = 'c').set(title = "GDP per Capita vs Total GDP")<jupyter_output><empty_output><jupyter_text>Plotting Billionaires networth and age<jupyter_code>plt.figure(figsize = (10,6))
sns.regplot(data =Bnaires, y = "networth", x = "age", color ='g')<jupyter_output><empty_output><jupyter_text>Merging total capita and billionaires data<jupyter_code>Bnaires_GDP =Bnaires.merge(total_capita,how= "inner", left_on = "country", right_on="Country")
countrygroup = Bnaires_GDP.groupby('country').sum()
countrygroup.head()
sns.jointplot(x= 'CAP', y = 'networth', data= countrygroup, kind='reg')
sns.jointplot(x= 'networth', y = 'GDP', data= countrygroup, kind='reg')<jupyter_output><empty_output><jupyter_text>Below plot against networth and total gdp/gdp_cap ratio does not provide good insights.<jupyter_code>plt.figure(figsize =(10,6))
sns.regplot(data = countrygroup, y = "networth", x = "TotalGDP_standardliving_ratio", color = 'c').set(title = "GDP per Capita vs Total GDP")<jupyter_output><empty_output><jupyter_text>ranking the data by gdp/gdp_cap to see if this provides a better information on billionaire countries and Gdp/gdp_cap gap<jupyter_code>countrygroup["gdp_gdpcap_gap_rank"] = countrygroup["TotalGDP_standardliving_ratio"].rank(ascending = True)
countrygroup["networth_rank"] = countrygroup["networth"].rank(ascending = False)
countrygroup.sort_values(by = "gdp_gdpcap_gap_rank", ascending= False)
countrygroup.reset_index(inplace = True)<jupyter_output><empty_output><jupyter_text>Below line graph on gdp_capita_gap_rank vs networth_rank has negative relationship, which indicates the countries that have most money by bilionaires will have higher proportion of gdp/gdpcapita<jupyter_code>sns.lmplot(data =countrygroup, y = "networth_rank", x ='gdp_gdpcap_gap_rank' )
top30networth= countrygroup.nlargest(30,["networth"])
sns.lmplot(data =top30networth, x = "networth_rank", y ='gdp_gdpcap_gap_rank', hue = 'country')


def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x']+.02, point['y'], str(point['val']))

label_point(top30networth["networth_rank"], top30networth["gdp_gdpcap_gap_rank"], top30networth["country"], plt.gca()) 
plt.gcf().set_size_inches(20, 15)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15238376674546888, 'fraction_numerical': 0.025512214342001575, 'mean_word_length': 6.487463126843658, 'pattern_counts': {'":': 1, '<': 69, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '665754', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 3865, 'n_tokens_neox': 3627, 'n_words': 772, 'path': 'Data with Python/1.GDP and Billionaires/GDP_Billionaires_05172022.ipynb', 'repo_name': 'suhsunghee/suhsunghee.github.io', 'size': '10152'}