Python - Json列表到Pandas数据框架

p5cysglq  于 2023-02-02  发布在  Python
关注(0)|答案(2)|浏览(132)

我有一个json列表,我不能转换为Pandas Dataframe (各行和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转换为dataframe:

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行?

jdgnovmf

jdgnovmf1#

如果在函数中更改此行:dfItem = jsonToDataFrame(data)至:
dfItem = pd.DataFrame.from_records(data)
我测试了你的函数,替换了这一行,使用['INAG']作为传递给getFinanceHistoricalStockFromByma函数的参数,它返回了一个DataFrame。

tnkciper

tnkciper2#

您可以直接在字典列表上调用pd.DataFrame(),如OP中的示例所示(.from_records()不是必需的)。

df = pd.DataFrame(data)

对于OP中的函数,由于pd.DataFrame.append()已被弃用,当前(panda〉= 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

相关问题