简体   繁体   English

Python - Json 列表到 Pandas Dataframe

[英]Python - Json List to Pandas Dataframe

I've a json list and I can't convert to Pandas dataframe (various rows and 19 columns)我有一个 json 列表,我无法转换为 Pandas dataframe(多行和 19 列)

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

json response: 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}
]

I use the next piece of code to convert this json to dataframe:我使用下一段代码将此 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)    

The result of json_normalize is 1 row and a lot of columns. json_normalize的结果是 1 行和很多列。 How can I convert this json response to 1 row per list?如何将此 json 响应转换为每个列表 1 行?

If you change this line in your function: dfItem = jsonToDataFrame(data) to: 如果在函数中更改此行: dfItem = jsonToDataFrame(data) to:

dfItem = pd.DataFrame.from_records(data)

it should work. 它应该工作。 I tested your function with this line replaced, using ['INAG'] as a parameter passed to your getFinanceHistoricalStockFromByma function, and it returned a DataFrame. 我使用['INAG']作为传递给你的getFinanceHistoricalStockFromByma函数的参数来测试你的函数替换了这一行,并返回了一个DataFrame。

You can directly call pd.DataFrame() directly on a list of dictionaries as in the sample in OP ( .from_records() is not necessary).您可以直接在字典列表上直接调用pd.DataFrame() ,就像 OP 中的示例一样( .from_records()不是必需的)。 Try:尝试:

df = pd.DataFrame(data)

For the function in the OP, since pd.DataFrame.append() is deprecated, the best way to write it currently (pandas >= 1.4.0) is to collect the json responses in a Python list and create a DataFrame once at the end of the loop.对于 OP 中的 function,由于pd.DataFrame.append()已弃用,目前编写它的最佳方法(pandas >= 1.4.0)是在 Python 列表中收集 json 响应,并在循环结束。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM