Creating the Boxplot
Source Code
import wbdata
import pandas as pd
countries = ["AFR","CHN","USA","DNK","FRA","LBN","LKA","LUX"]
import datetime
df = wbdata.get_dataframe({'NY.GDP.MKTP.KD.ZG': 'GDP Rate'}, country=countries)
df
Let’s also transfer the country labels to the columns with unstack(). If we give “country” as the argument it will leave the date index in place but turn the country index to columns.
df = df.unstack("country")
df
We need to also fix the columns, look at what happens when we use the following code.
df.columns
There is a function droplevel() which can help us out here, look what happens when we now call the below code.
df.columns.droplevel()
Set the columns equal to it.
df.columns = df.columns.droplevel()
df
To create a box plot we use sns.boxplot()
import seaborn as sns
import matplotlib.pyplot as plt
sns.boxplot(data=df)
plt.title("GDP Growth Rate")
plt.show()
Let’s limit the allowed values to be from -10% to 20%.
sns.boxplot(data=df[(df < 20) & (df > -10)])
plt.title("GDP Growth Rate")
plt.show()
Let’s also look at 2 standard deviations.
sns.boxplot(data=df[(df<(df.mean()+df.std()*2)) & (df>(df.mean()-df.std()*2))])
plt.title("GDP Growth Rate")
plt.show()
Source Code