Data Frame with information from the Central Bank of Brazil

Asked

Viewed 143 times

2

I created a function to collect data from the Central Bank by separating by variables that I want to work. Now I wanted to create a data frame that had all this information, but I’m not getting it, if anyone can help me thank you.

def consulta_bc(codigo_bc):
    url = 'http://api.bcb.gov.br/dados/serie/bcdata.sgs.{}/dados?formato=json'.format(codigo_bc)
    df = pd.read_json(url)
    df['data'] = pd.to_datetime(df['data'], dayfirst = True)
    df.set_index('data', inplace = True)
    return df.loc['2015-01-01' : '2020-09-01']

pib = consulta_bc(4380)
tx_selic = consulta_bc(4390)
desemprego = consulta_bc(24369) 
m1 = consulta_bc(27791)
igpm = consulta_bc(189)

data = [[pib, tx_selic, desemprego, m1, igpm]]

index = pd.date_range(start = '2015-01-01', end = '2020-09-01', freq = "M")

df = pd.DataFrame(data, columns = [['Pib', 'Selic', 'Desemprego', 'M1', 'IGP-M']], index = index)

df

inserir a descrição da imagem aqui

  • 2

    Describing the general problem you will get only a general answer. Example: "How to build a house?" , "Use blocks and cement, build solid walls". Instead ask something specific and responsive in a useful way: "How to lift a wall using this type of block with such a slope and such height safely?" , answer: "Position the blocks in such format, run this block placement algorithm, do not use this tool because there is such a risk, here is an example running from a wall ready for you to see how it does [link]". See? Too many wide questions don’t help.

1 answer

3


How your data is already DataFrames with the same contents, you can simply concatenate them by column using pd.concat:

data = [pib, tx_selic, desemprego, m1, igpm]
df = pd.concat(data, axis=1)
df.columns = ['Pib', 'Selic', 'Desemprego', 'M1', 'IGP-M']

Output:

                 Pib  Selic  Desemprego         M1  IGP-M
data                                                     
2015-01-01  481097.2   0.94         6.8  333454078   0.76
2015-02-01  466951.8   0.82         7.4  330708611   0.27
2015-03-01  508621.8   1.04         7.9  327928960   0.98
2015-04-01  493434.8   0.95         8.0  319444733   1.17
2015-05-01  491259.3   0.99         8.1  318441737   0.41
...              ...    ...         ...        ...    ...
2020-05-01  560059.1   0.24        12.9  503444375   0.28
2020-06-01  605146.9   0.21        13.3  534044506   1.56
2020-07-01  631040.8   0.19        13.8  536124166   2.23
2020-08-01  629988.0   0.16        14.4  553487667   2.74
2020-09-01  630706.5   0.16        14.6  565442300   4.34

[69 rows x 5 columns]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.