Json 列表到 Pandas Dataframe

问题描述 投票:0回答:2

我有一个 json 列表,但无法转换为 Pandas 数据框(多行和 19 列)

回复链接:https://www.byma.com.ar/wp-admin/admin-ajax.php?action=get_historico_simbolo&simbolo=INAG&fecha=01-02-2018

json 响应:

[
    {"Apertura":35,"Apertura_Homogeneo":35,"Cantidad_Operaciones":1,"Cierre":35,"Cierre_Homogeneo":35,"Denominacion":"INSUMOS AGROQUIMICOS S.A.","Fecha":"02\/02\/2018","Maximo":35,"Maximo_Homogeneo":35,"Minimo":35,"Minimo_Homogeneo":35,"Monto_Operado_Pesos":175,"Promedio":35,"Promedio_Homogeneo":35,"Simbolo":"INAG","Variacion":-5.15,"Variacion_Homogeneo":0,"Vencimiento":"48hs","Volumen_Nominal":5},
    {"Apertura":34.95,"Apertura_Homogeneo":34.95,"Cantidad_Operaciones":2,"Cierre":34.95,"Cierre_Homogeneo":34.95,"Denominacion":"INSUMOS AGROQUIMICOS S.A.","Fecha":"05\/02\/2018","Maximo":34.95,"Maximo_Homogeneo":34.95,"Minimo":34.95,"Minimo_Homogeneo":34.95,"Monto_Operado_Pesos":5243,"Promedio":-79228162514264337593543950335,"Promedio_Homogeneo":-79228162514264337593543950335,"Simbolo":"INAG","Variacion":-0.14,"Variacion_Homogeneo":-0.14,"Vencimiento":"48hs","Volumen_Nominal":150},
    {"Apertura":32.10,"Apertura_Homogeneo":32.10,"Cantidad_Operaciones":2,"Cierre":32.10,"Cierre_Homogeneo":32.10,"Denominacion":"INSUMOS AGROQUIMICOS S.A.","Fecha":"07\/02\/2018","Maximo":32.10,"Maximo_Homogeneo":32.10,"Minimo":32.10,"Minimo_Homogeneo":32.10,"Monto_Operado_Pesos":98756,"Promedio":32.10,"Promedio_Homogeneo":32.10,"Simbolo":"INAG","Variacion":-8.16,"Variacion_Homogeneo":-8.88,"Vencimiento":"48hs","Volumen_Nominal":3076}
]

我使用下一段代码将此 json 转换为数据帧:

def getFinanceHistoricalStockFromByma(tickerList): 
     dataFrameHistorical = pd.DataFrame()  
     for item in tickerList:
         url = 'https://www.byma.com.ar/wp-admin/admin-ajax.php?action=get_historico_simbolo&simbolo=' + item + '&fecha=01-02-2018'
         response = requests.get(url)
         if response.content : print 'ok info Historical Stock'
         data = response.json()                
         dfItem = jsonToDataFrame(data)                
         dataFrameHistorical = dataFrameHistorical.append(dfItem, ignore_index=True)    
    return dataFrameHistorical

def jsonToDataFrame(jsonStr):    
     return json_normalize(jsonStr)    

json_normalize
的结果是1行和很多列。如何将此 json 响应转换为每个列表 1 行?

python json pandas dataframe python-requests
2个回答
84
投票

如果您将函数中的这一行:

dfItem = jsonToDataFrame(data)
更改为:

dfItem = pd.DataFrame.from_records(data)

它应该可以工作。我测试了您的函数,替换了这一行,使用 ['INAG'] 作为传递给您的

getFinanceHistoricalStockFromByma
函数的参数,它返回了一个 DataFrame。


5
投票

您可以像OP中的示例一样直接在字典列表上直接调用

pd.DataFrame()
(不需要
.from_records()
)。尝试:

df = pd.DataFrame(data)

对于OP中的函数,由于

pd.DataFrame.append()
已被弃用,目前(pandas >= 1.4.0)编写它的最佳方法是收集Python列表中的json响应,并在列表末尾创建一个DataFrame。循环。

def getFinanceHistoricalStockFromByma(tickerList): 
    dataHistorical = []                                 # <----- list
    for item in tickerList:
        url = 'https://www.byma.com.ar/wp-admin/admin-ajax.php?action=get_historico_simbolo&simbolo=' + item + '&fecha=01-02-2018'
        response = requests.get(url)
        if response.content:
            print('ok info Historical Stock')
        data = response.json()
        dataHistorical.append(data)                     # <----- list.append()
    dataFrameHistorical = pd.DataFrame(dataHistorical)  # <----- dataframe construction
    return dataFrameHistorical
© www.soinside.com 2019 - 2024. All rights reserved.