如何将 Pandas Dataframe 转换为所需的 Json 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20639631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Pandas Dataframe to the desired Json format
提问by zsljulius
start = datetime.datetime(2013, 1, 1)
end = datetime.datetime(2013, 01, 27)
f=web.get_data_yahoo('AAPL',start, end)
f['Adj Close'].to_json(date_format='iso',orient='split')
The above code gives the following result:
上面的代码给出了以下结果:
Out[85]: '{"name":"Adj Close","index":["2013-01-02T00:00:00","2013-01-03T00:00:0
0","2013-01-04T00:00:00","2013-01-07T00:00:00","2013-01-08T00:00:00","2013-01-09
T00:00:00","2013-01-10T00:00:00","2013-01-11T00:00:00","2013-01-14T00:00:00","20
13-01-15T00:00:00","2013-01-16T00:00:00","2013-01-17T00:00:00","2013-01-18T00:00
:00","2013-01-22T00:00:00","2013-01-23T00:00:00","2013-01-24T00:00:00","2013-01-
25T00:00:00"],"data":[535.58,528.82,514.09,511.06,512.44,504.43,510.68,507.55,48
9.45,474.01,493.69,490.36,487.75,492.4,501.41,439.46,429.1]}'
What I want is:
我想要的是:
'[{"index":"2013-01-02T00:00:00",value:535.58},{"index":"2013-01-04T00:00:00",value:528.82},...]'
Is this possible? How should I go around this?
这可能吗?我应该如何解决这个问题?
采纳答案by Andy Hayden
It looks like this could be a useful alternative method for to_json, for the moment, one workaround is to read it back into python and munge :s
看起来这可能是 to_json 的一种有用的替代方法,目前,一种解决方法是将其读回 python 和 munge :s
In [11]: s = f['Adj Close'].to_json(date_format='iso',orient='split')
In [12]: d = json.loads(s) # import json
In [13]: [{"index": date, "value": val} for date, val in zip(d['index'], d['data'])]
Out[13]:
[{'index': u'2013-01-02T00:00:00.000Z', 'value': 535.58},
{'index': u'2013-01-03T00:00:00.000Z', 'value': 528.82},
{'index': u'2013-01-04T00:00:00.000Z', 'value': 514.09},
{'index': u'2013-01-07T00:00:00.000Z', 'value': 511.06},
{'index': u'2013-01-08T00:00:00.000Z', 'value': 512.44},
{'index': u'2013-01-09T00:00:00.000Z', 'value': 504.43},
{'index': u'2013-01-10T00:00:00.000Z', 'value': 510.68},
{'index': u'2013-01-11T00:00:00.000Z', 'value': 507.55},
{'index': u'2013-01-14T00:00:00.000Z', 'value': 489.45},
{'index': u'2013-01-15T00:00:00.000Z', 'value': 474.01},
{'index': u'2013-01-16T00:00:00.000Z', 'value': 493.69},
{'index': u'2013-01-17T00:00:00.000Z', 'value': 490.36},
{'index': u'2013-01-18T00:00:00.000Z', 'value': 487.75},
{'index': u'2013-01-22T00:00:00.000Z', 'value': 492.4},
{'index': u'2013-01-23T00:00:00.000Z', 'value': 501.41},
{'index': u'2013-01-24T00:00:00.000Z', 'value': 439.46},
{'index': u'2013-01-25T00:00:00.000Z', 'value': 429.1}]
In [14]: json.dumps([{"index": date, "value": val} for date, val in zip(d['index'], d['data'])])
Out[14]: '[{"index": "2013-01-02T00:00:00.000Z", "value": 535.58}, {"index": "2013-01-03T00:00:00.000Z", "value": 528.82}, {"index": "2013-01-04T00:00:00.000Z", "value": 514.09}, {"index": "2013-01-07T00:00:00.000Z", "value": 511.06}, {"index": "2013-01-08T00:00:00.000Z", "value": 512.44}, {"index": "2013-01-09T00:00:00.000Z", "value": 504.43}, {"index": "2013-01-10T00:00:00.000Z", "value": 510.68}, {"index": "2013-01-11T00:00:00.000Z", "value": 507.55}, {"index": "2013-01-14T00:00:00.000Z", "value": 489.45}, {"index": "2013-01-15T00:00:00.000Z", "value": 474.01}, {"index": "2013-01-16T00:00:00.000Z", "value": 493.69}, {"index": "2013-01-17T00:00:00.000Z", "value": 490.36}, {"index": "2013-01-18T00:00:00.000Z", "value": 487.75}, {"index": "2013-01-22T00:00:00.000Z", "value": 492.4}, {"index": "2013-01-23T00:00:00.000Z", "value": 501.41}, {"index": "2013-01-24T00:00:00.000Z", "value": 439.46}, {"index": "2013-01-25T00:00:00.000Z", "value": 429.1}]'
Obviously this defeats the purpose of an efficient to_json function, but I think it's worth adding this as a feature request- I thinkthis is a fairly standard format, we just overlooked it.
显然,这违背了高效 to_json 函数的目的,但我认为值得将其添加为功能请求- 我认为这是一种相当标准的格式,我们只是忽略了它。
回答by u7866024
This articlemay help you to sovle this problem. You can write like this:
这篇文章可以帮助你解决这个问题。你可以这样写:
f['Adj Close'].to_json(orient="records")
In the article above we can see:
在上面的文章中,我们可以看到:
records : list like [{column -> value}, ... , {column -> value}]
I solved the problem this way.
我是这样解决问题的。
回答by Mikhail_Sam
It works as it mentioned above, but I found one more interesting way for Pretty output format:
它的工作原理如上所述,但我发现了一种更有趣的Pretty输出格式方式:
response = make_response(f['Adj Close'].to_json(orient='records'))
response.headers['Content-Type'] = 'application/json'
return response
It this way you get JSON and also use header, so reader will know the right format.
通过这种方式,您可以获得 JSON 并使用标头,因此读者将知道正确的格式。

