'Create multiple DataFrames using data from an api

I'm using the world bank API to analyze data and I want to create multiple data frames with the same indicators for different countries.

import wbgapi as wb
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import time as t_lib

#Variables

indicators = ['AG.PRD.LVSK.XD', 'AG.YLD.CREL.KG', 'NY.GDP.MINR.RT.ZS', 'SP.POP.TOTL.FE.ZS']
countries = ['PRT', 'BRL', 'ARG']
time = range(1995, 2021)

#Code

def create_df(country):
    df = wb.data.DataFrame(indicators, country, time, labels = True).reset_index()
    columns = [ item for item in df['Series'] ]
    columns
    df = df.T
    df.columns = columns 
    df.drop(['Series', 'series'], axis= 0, inplace = True)
    df = df.reset_index()
    return df 

 
list_of_dfs = []

for n in range(len(countries)):
    var = create_df(countries[n])
    list_of_dfs.append(var)

Want I really wanted is to create a data frame with a different name for a different country and to store them in a list or dict like: [df_1, df_2, df_3...]

EDIT:

I'm trying this now:

a_dictionary = {}

for n in range(len(countries)):
    a_dictionary["key%s" %n] = create_df(countries[n])

It was suppose to work but still get the same error in the 2nd loop:

APIResponseError: APIError: JSON decoding error (https://api.worldbank.org/v2/en/sources/2/series/AG.PRD.LVSK.XD;AG.YLD.CREL.KG;NY.GDP.MINR.RT.ZS;SP.POP.TOTL.FE.ZS/country/BRL/time/YR1995;YR1996;YR1997;YR1998;YR1999;YR2000;YR2001;YR2002;YR2003;YR2004;YR2005;YR2006;YR2007;YR2008;YR2009;YR2010;YR2011;YR2012;YR2013;YR2014;YR2015;YR2016;YR2017;YR2018;YR2019;YR2020?per_page=1000&page=1&format=json)

UPDATE:

Thanks to notiv I noticed the problema was in "BRA" instead of "BRL". I'm also putting here a new approach that works as well by creating a master dataframe and then slicing it by country to create the desired dataframes:

df = wb.data.DataFrame(indicators, countries, time, labels = True).reset_index()
columns = [ item for item in df['Series'] ]
columns
df = df.T
df.columns = columns 
df.drop(['Series', 'series'], axis= 0, inplace = True)
df = df.reset_index()
df

a_dictionary = {}

for n in range(len(countries)):
    
    new_df = df.loc[: , (df == countries[n]).any()]
    new_df['index'] = df['index']
    new_df.set_index('index', inplace = True)
    new_df.drop(['economy', 'Country'], axis= 0, inplace = True)
    a_dictionary["eco_df%s" %n] = new_df

 
for loop in range(len(countries)):

    for n in range(len(a_dictionary[f'eco_df{loop}'].columns)):
        sns.set_theme(style="dark")
        g = sns.relplot( data= a_dictionary[f'eco_df{loop}'], x= a_dictionary[f'eco_df{loop}'].index, y= a_dictionary[f'eco_df{loop}'].iloc[:,n], kind="line", palette="crest",
                    height=5, aspect=1.61, legend=False).set(title=countries[loop])

        g.set_axis_labels("Years")
        g.set_xticklabels(rotation=45)
        g.tight_layout()

At the end I used the dataframes to create a chart for each indicator for each country. Many thanks for the help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source