使用Python / Pandas使用JSON嵌套在JSON中

时间:2021-04-11 22:55:48

I am trying to load JSON data using python, however, it looks like this:

我试图使用python加载JSON数据,但是,它看起来像这样:

{
    "instrument" : "EUR_USD",
    "granularity" : "D",
    "candles" : [
        {
            "time" : "2014-07-02T04:00:00.000000Z", // time in RFC3339 format
            "openMid" : 1.36803,
            "highMid" : 1.368125,
            "lowMid" : 1.364275,
            "closeMid" : 1.365315,
            "volume" : 28242,
            "complete" : true
        },
        {
            "time" : "2014-07-03T04:00:00.000000Z", // time in RFC3339 format
            "openMid" : 1.36532,
            "highMid" : 1.366445,
            "lowMid" : 1.35963,
            "closeMid" : 1.3613,
            "volume" : 30487,
            "complete" : false
        }
    ]
}

My problem is that when I load it using Pandas, instrument, granularity, and candles are processed as the column titles. However, I want to use time, openMid, highMid, lowMid, closeMid, volume, and complete to create my columns. But they are just processed as a belonging to candles. Any ideas on how I can accomplish this? Thanks

我的问题是,当我使用Pandas加载它时,仪器,粒度和蜡烛将作为列标题处理。但是,我想使用time,openMid,highMid,lowMid,closeMid,volume和complete来创建我的列。但它们只是作为蜡烛的归属处理。关于如何实现这一目标的任何想法?谢谢

1 个解决方案

#1


1  

You'll have to read the string using the json library first:

您必须首先使用json库读取字符串:

import json
data = json.loads(string)

And then you can extract the candles data from the resulting dictionary and build your DataFrame that way, e.g.:

然后你可以从结果字典中提取蜡烛数据并以这种方式构建你的DataFrame,例如:

candles_data = data.pop('candles')
df = pd.DataFrame(candles_data)
for k, v in data.iteritems():
    df[k] = v

#1


1  

You'll have to read the string using the json library first:

您必须首先使用json库读取字符串:

import json
data = json.loads(string)

And then you can extract the candles data from the resulting dictionary and build your DataFrame that way, e.g.:

然后你可以从结果字典中提取蜡烛数据并以这种方式构建你的DataFrame,例如:

candles_data = data.pop('candles')
df = pd.DataFrame(candles_data)
for k, v in data.iteritems():
    df[k] = v