问题描述
我正在尝试使用python加载JSON数据,但是它看起来像这样:
I am trying to load JSON data using python, however, it looks like this:
{
"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
}
]
}
我的问题是,当我使用熊猫加载时,仪器,粒度和蜡烛被作为列标题处理.但是,我想使用 time,openMid,highMid,lowMid,closeMid,volume和complete 来创建我的列.但是它们只是被当作蜡烛的一部分来处理.关于如何实现此目标的任何想法?谢谢
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
推荐答案
您必须首先使用json
库读取字符串:
You'll have to read the string using the json
library first:
import json
data = json.loads(string)
然后您可以从生成的字典中提取蜡烛数据并以这种方式构建DataFrame,例如:
And then you can extract the candles data from the resulting dictionary and build your DataFrame that way, e.g.:
candles_data = data.pop('candles')
df = pd.DataFrame(candles_data)
for k, v in data.iteritems():
df[k] = v
这篇关于使用Python/Pandas处理嵌套在JSON中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!