簡體   English   中英

我無法從 api 正確地看到 json dataframe

[英]I can't correctly visualize a json dataframe from api

我目前正在嘗試從公共 API 讀取一些數據。 It has different ways of reading (json, csv, txt, among others), just change the label in the url (/ json, / csv, / txt...). url如下:

https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/csv/ https://estadisticas.bcrp.gob.pe/estadisticas/series/...api/PN01

我的問題是,當嘗試導入 Pandas dataframe 時,它沒有正確讀取數據。 我正在嘗試以下替代方案:

import pandas as pd
import requests

url = 'https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/json/'
r = requests.get(url)

rjson = r.json()

df= json_normalize(rjson)
df['periods']

在此處輸入圖像描述

我也嘗試讀取 csv 格式的數據:

import pandas as pd
import requests

url = 'https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/csv/'

collisions = pd.read_csv(url, sep='<br>')
collisions.head()

但是我沒有得到好的結果; dataframe 無法正確顯示,因為“周期”列與所有值分組......

output顯示如下:

在此處輸入圖像描述

所有數據顯示為列:/

以下是如何正確顯示數據的示例:

在此處輸入圖像描述

您建議嘗試什么替代方案?

提前感謝您的時間和幫助!

我會注意你的回答,問候!

對於csv您可以使用StringIO io中的 StringIO

In [20]: import requests

In [21]: res = requests.get("https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/csv/")

In [22]: import pandas as pd

In [23]: import io

In [24]: df = pd.read_csv(io.StringIO(res.text.strip().replace("<br>","\n")), engine='python')

In [25]: df
Out[25]:
   Mes/A&ntilde;o  Tipo de cambio - promedio del periodo (S/ por US$) - Bancario - Promedio
0        Jul.2018                                           3.276595
1        Ago.2018                                           3.288071
2        Sep.2018                                           3.311325
3        Oct.2018                                           3.333909
4        Nov.2018                                           3.374675
5        Dic.2018                                           3.364026
6        Ene.2019                                           3.343864
7        Feb.2019                                           3.321475
8        Mar.2019                                           3.304690
9        Abr.2019                                           3.303825
10       May.2019                                           3.332364
11       Jun.2019                                           3.325650
12       Jul.2019                                           3.290214
13       Ago.2019                                           3.377560
14       Sep.2019                                           3.357357
15       Oct.2019                                           3.359762
16       Nov.2019                                           3.371700
17       Dic.2019                                           3.355190
18       Ene.2020                                           3.327364
19       Feb.2020                                           3.390350
20       Mar.2020                                           3.491364
21       Abr.2020                                           3.397500
22       May.2020                                           3.421150
23       Jun.2020                                           3.470167

呃,抱歉找不到里面有多個對象的讀取 json 的鏈接。 問題是我們不能對這種格式使用 load/s。 所以必須改用raw_decode()

這段代碼應該可以工作

import pandas as pd
import json
import urllib.request as ur
from pprint import pprint

d = json.JSONDecoder()
url = 'https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/json/'

#reading and transforming json into list of dictionaries
data = []
with ur.urlopen(url) as json_file:
    x = json_file.read().decode() # decode to convert bytes string into normal string
    while True:
        try:
            j, n = d.raw_decode(x)
        except ValueError:
            break
        #print(j)
        data.append(j)
        x = x[n:]
#pprint(data)

#creating list of dictionaries to convert into dataframe
clean_list = []
for i, d in enumerate(data[0]['periods']):
    dict_data = {
        "month_year": d['name'],
        "value": d['values'][0],
    }
    clean_list.append(dict_data)
#print(clean_list)

#pd.options.display.width = 0
df = pd.DataFrame(clean_list)
print(df)

結果

   month_year             value
0    Jul.2018  3.27659523809524
1    Ago.2018  3.28807142857143
2    Sep.2018          3.311325
3    Oct.2018  3.33390909090909
4    Nov.2018          3.374675
5    Dic.2018  3.36402631578947
6    Ene.2019  3.34386363636364
7    Feb.2019          3.321475
8    Mar.2019  3.30469047619048
9    Abr.2019          3.303825
10   May.2019  3.33236363636364
11   Jun.2019           3.32565
12   Jul.2019  3.29021428571428
13   Ago.2019           3.37756
14   Sep.2019  3.35735714285714
15   Oct.2019   3.3597619047619
16   Nov.2019            3.3717
17   Dic.2019  3.35519047619048
18   Ene.2020  3.32736363636364
19   Feb.2020           3.39035
20   Mar.2020  3.49136363636364
21   Abr.2020            3.3975
22   May.2020           3.42115
23   Jun.2020  3.47016666666667

如果我以某種方式再次找到該鏈接,我將編輯/評論我的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM