如何将嵌套的JSON API响应转换为Python中的数据框

huangapple go评论78阅读模式
英文:

How to convert nested JSON API reponse to dataframe in python

问题

我有一个API,返回类似以下列表的数据:

{'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55}, 'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15}, 'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1}, 'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85}, 'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}}

当我尝试将其转换为数据框时,主要值会变为以下样式的标题:
              NFO:AXISBANK23JUNFUT  ...  NFO:BALRAMCHIN23JUNFUT

instrument_token 9156098.00 ... 9180930.00
last_price 967.55 ... 395.95

但我需要将这些数据以以下方式反转:
                 instrument_token      last_price

NFO:AXISBANK23JUNFUT 9156098 967.55
NFO:BALRAMCHIN23JUNFUT 9180930 395.95

英文:

I have one API which returns data for a list something like below:

{'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55}, 'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15}, 'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1}, 'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85}, 'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}}

When I try to convert it to dataframe, primary values goes in header something like below:

                  NFO:AXISBANK23JUNFUT  ...  NFO:BALRAMCHIN23JUNFUT
instrument_token            9156098.00  ...              9180930.00
last_price                      967.55  ...                  395.95

But I need to have this data available in opposite way as below:

                         instrument_token      last_price
NFO:AXISBANK23JUNFUT      9156098               967.55
NFO:BALRAMCHIN23JUNFUT    9180930               395.95

答案1

得分: 0

首先,您需要将键设置为索引,将其他嵌套数据设置为列。您可以使用以下代码片段 Ref: Pandas from_records

samples = {'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55}, 'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15}, 'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1}, 'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85}, 'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}}
# 将字典的键设置为索引列,嵌套值设置为记录。
df = pd.DataFrame.from_records(list(samples.values()), index=list(samples.keys()))

不包括代码部分的翻译。

英文:

First your need to set the keys as index and the other nested data as columns.
You can use the following snippet Ref: Pandas from_records

samples = {'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55}, 'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15}, 'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1}, 'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85}, 'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}}
# Set the dictionary keys as index column and the nesting values as records.
df = pd.DataFrame.from_records(list(samples.values()), index=list(samples.keys()))

答案2

得分: 0

首先,我将其转换为数据框(DF),然后使用DF的"Transpose"方法进行转换,这样我的数据就被转换为所需的格式。然后我可以使用脚本名称作为索引来查找数据。

英文:

First I converted into a DF and then used DF Tanspose method to convert this way my data got converted to desired format. and then I can use the script names as index for finding the data.

答案3

得分: 0

orient参数被设置为'index',用于指定字典的键应该作为DataFrame的索引。

import pandas as pd

api_response = {
    'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55},
    'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15},
    'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1},
    'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85},
    'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}
}

df = pd.DataFrame.from_dict(api_response, orient='index')
英文:

you can use this
The orient='index' parameter is set to specify that the keys of the dictionary should be used as the index of the DataFrame.

import pandas as pd

api_response = {
'NFO:AXISBANK23JUNFUT': {'instrument_token': 9156098, 'last_price': 967.55},
'NFO:BAJAJ-AUTO23JUNFUT': {'instrument_token': 9156354, 'last_price': 4619.15},
'NFO:BAJAJFINSV23JUNFUT': {'instrument_token': 9179138, 'last_price': 1544.1},
'NFO:BAJFINANCE23JUNFUT': {'instrument_token': 9179394, 'last_price': 7390.85},
'NFO:BALRAMCHIN23JUNFUT': {'instrument_token': 9180930, 'last_price': 395.95}

}

df = pd.DataFrame.from_dict(api_response, orient='index')

huangapple
  • 本文由 发表于 2023年6月19日 21:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定