#Data cleaning
import pandas as pd
import numpy as np
#Data Visualization
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def country_df(country_name,reqcol):
#loading data
df=pd.read_csv('top_six_economies.csv')
#querry data based on the given country name
country_df=df.loc[(df["Country Name"]==country_name)]
country=country_df.copy()
country.drop('Unnamed: 0',axis=1,inplace=True)
country.set_index('Country Name')
return country[reqcol]
col=['Country Name','Year','GDP (current US$)','GDP, PPP (current international $)',
'GDP per capita (current US$)','GDP growth (annual %)',
'Imports of goods and services (% of GDP)',
'Exports of goods and services (% of GDP)',
'Unemployment, total (% of total labor force) (modeled ILO estimate)',
'Population, total','Population growth (annual %)',
'Life expectancy at birth, total (years)',
'Poverty headcount ratio at $1.90 a day (2011 PPP) (% of population)']
#United States of America
usa=country_df(country_name="United States",reqcol=col)
#China
china=country_df(country_name="China",reqcol=col)
usa.head()
usa.describe()
usa.info()
china.head()
china.describe()
#create subplots
var1 = make_subplots(rows=1, cols=2,
subplot_titles=["USA","CHINA"])
#add traces to the subplots
var1.add_trace(
go.Scatter(x=usa["Year"], y=usa['GDP (current US$)'],name="GDP nominal (usa)"),
row=1, col=1
)
var1.add_trace(
go.Scatter(x=china["Year"], y=china['GDP (current US$)'],name="GDP nominal (china)"),
row=1, col=2
)
#Update axes title
#USA
var1.update_yaxes(title_text="GDP Nominal in Trillions", row=1, col=1)
var1.update_xaxes(title_text="Year", row=1, col=1)
#CHINA
var1.update_yaxes(title_text="GDP Nominal in Trillions", row=1, col=2)
var1.update_xaxes(title_text="Year", row=1, col=2)
# Update title
var1.update_layout(title_text='Nominal GDP ($) trend of USA and CHINA from 1991 to 2020 ',
title={'x':0.5},
font_family="Times New Roman",
title_font_family="Times New Roman")
var1.show()
#Lineplot (usa)
line1= px.line(usa, x="Year", y="GDP growth (annual %)")
line1.update_layout(title={"text":"Annual GDP growth percent (USA) from 1991 to 2020",
'y':0.9,
'x':0.5,},
font_family="Times New Roman",
title_font_family="Times New Roman",)
line1.show()
#USA
fig = px.histogram(usa, x="GDP growth (annual %)",text_auto=True)
fig.update_layout(title={"text":"Histogram plot for annual GDP Growth rate of USA from 1991 to 2020",
'y':0.9,
'x':0.5,},
font_family="Times New Roman",
title_font_family="Times New Roman",
xaxis_title="GDP growth rate range in (%)",
yaxis_title="Frequency",
bargap=0.1)
fig.show()
#Lineplot (china)
line2= px.line(china, x="Year", y="GDP growth (annual %)")
line2.update_layout(title={"text":"Annual GDP growth percent CHINA from 1991 to 2020",
'y':0.9,
'x':0.5,},
font_family="Times New Roman",
title_font_family="Times New Roman",)
line2.show()
#CHINA
fig0 = px.histogram(china, x="GDP growth (annual %)",text_auto=True)
#Updating plot layout
fig0.update_layout(title={"text":"Histogram plot for annual GDP Growth rate of CHINA from 1991 to 2020",
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'},
xaxis_title="GDP growth rate range in (%)",
yaxis_title="Frequency",
bargap=0.1)
fig0.show()
# Initialize figure with subplots
fig1 = make_subplots(rows=1, cols=2,
subplot_titles=["USA","CHINA" ],
specs=[[{"secondary_y": True}, {"secondary_y": True}]])
# Add traces
#USA subplot
# Top left
fig1.add_trace(
go.Scatter(x=usa["Year"], y=usa['GDP (current US$)'], name="GDP Nominal in trillions (USA)"),
row=1, col=1, secondary_y=False)
fig1.add_trace(
go.Scatter(x=usa["Year"], y=usa['Unemployment, total (% of total labor force) (modeled ILO estimate)'],
name="Unemployment percentage in total labor force in % (USA)"),
row=1, col=1, secondary_y=True,
)
#CHINA Subplot
# Top right
fig1.add_trace(
go.Scatter(x=china["Year"], y=china['GDP (current US$)'], name="GDP Nominal in trillions (CHINA)"),
row=1, col=2, secondary_y=False,
)
fig1.add_trace(
go.Scatter(x=china["Year"], y=china['Unemployment, total (% of total labor force) (modeled ILO estimate)'],
name="Unemployment percentage in total labor force in % (CHINA)"),
row=1, col=2, secondary_y=True,
)
#X-axis properties
#USA
fig1.update_xaxes(title_text="Year", row=1, col=1)
#CHINA
fig1.update_xaxes(title_text="Year", row=1, col=2)
#Y-axis properties
#USA
fig1.update_yaxes(title_text="GDP Nominal(in trillions)", row=1, col=1,secondary_y=False)
fig1.update_yaxes(title_text="Unemployment(in %)", row=1, col=1,secondary_y=True)
#CHINA
fig1.update_yaxes(title_text="GDP Nominal(in trillions)", row=1, col=2,secondary_y=False)
fig1.update_yaxes(title_text="Unemployment(in %)", row=1, col=2,secondary_y=True)
# Update title
fig1.update_layout(title_text='Comparison of Nominal GDP ($) and Unemployment percentage in total labor force from 1991 to 2020 ',
font_family="Times New Roman",
title_font_family="Times New Roman")
fig1.show()
# Initialize figure with subplots
fig2 = make_subplots(rows=1, cols=2,
vertical_spacing=0.001,
subplot_titles=["USA","CHINA" ],
specs=[[{"secondary_y": True}, {"secondary_y": True}]])
# Add traces
#USA subplot
# Top left
fig2.add_trace(
go.Scatter(x=usa["Year"], y=usa['Imports of goods and services (% of GDP)'], name="Imports of goods and services in % (USA)"),
row=1, col=1, secondary_y=False)
fig2.add_trace(
go.Scatter(x=usa["Year"], y=usa['Exports of goods and services (% of GDP)'],
name="Exports of goods and services in % (USA)"),
row=1, col=1, secondary_y=True,
)
#CHINA subplot
# Top right
fig2.add_trace(
go.Scatter(x=china["Year"], y=china['Imports of goods and services (% of GDP)'], name="Imports of goods and services in % (CHINA)"),
row=1, col=2, secondary_y=False,
)
fig2.add_trace(
go.Scatter(x=china["Year"], y=china['Exports of goods and services (% of GDP)'],
name="Exports of goods and services in % (CHINA)"),
row=1, col=2, secondary_y=True,
)
#X-axis properties
#USA
fig2.update_xaxes(title_text="Year", row=1, col=1)
#CHINA
fig2.update_xaxes(title_text="Year", row=1, col=2)
#Y-axis properties
#USA
fig2.update_yaxes(title_text="Imports percentage in total GDP ", row=1, col=1,secondary_y=False)
fig2.update_yaxes(title_text="Export percentage in total GDP", row=1, col=1,secondary_y=True)
#CHINA
fig2.update_yaxes(title_text="Imports percentage in total GDP ", row=1, col=2,secondary_y=False)
fig2.update_yaxes(title_text="Export percentage in total GDP", row=1, col=2,secondary_y=True)
#Update title
fig2.update_layout(title_text='Comparison of Import and Exports of good and services percentage in total GDP from 1991 to 2020',
font_family="Times New Roman",
title_font_family="Times New Roman")
fig2.show()