pandas 将数据帧输出到 json 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38688072/
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
output a dataframe to a json array
提问by John
I was wondering if there was a more efficient way to do the following operation.
我想知道是否有更有效的方法来执行以下操作。
# transforms datetime into timestamp in seconds
t = df.index.values.astype(np.int64) // 10**6
return jsonify(np.c_[t, df.open, df.high, df.low, df.close, df.volume].tolist())
where df
is a dataframe containing an index that is a date, and at least (but not only) the following attributes: open
, high
, low
, close
, volume
. I then output the newly created array as JSON with flask's jsonify
. The code above works but it looks pretty inefficient to me any idea on how to make it nicer/more efficient.
其中df
是一个包含日期索引的数据框,以及至少(但不仅限于)以下属性:open
, high
, low
, close
, volume
. 然后我将新创建的数组输出为 JSON,并带有flask 的jsonify
. 上面的代码有效,但对我来说,任何关于如何使其更好/更有效的想法看起来效率很低。
回答by MaxU
you can use to_json()method:
您可以使用to_json()方法:
In [88]: import pandas_datareader.data as web
In [89]: apl = web.get_data_yahoo('AAPL', '2016-07-05', '2016-07-07')
In [90]: apl
Out[90]:
Open High Low Close Volume Adj Close
Date
2016-07-05 95.389999 95.400002 94.459999 94.989998 27705200 94.989998
2016-07-06 94.599998 95.660004 94.370003 95.529999 30949100 95.529999
2016-07-07 95.699997 96.500000 95.620003 95.940002 25139600 95.940002
I'll use json.dumps(..., indent=2)
in order to make it nicer/readable:
我将使用json.dumps(..., indent=2)
以使其更好/可读:
In [91]: import json
orient='index'
东方='索引'
In [98]: print(json.dumps(json.loads(apl.to_json(orient='index')), indent=2))
{
"1467849600000": {
"Close": 95.940002,
"High": 96.5,
"Open": 95.699997,
"Adj Close": 95.940002,
"Volume": 25139600,
"Low": 95.620003
},
"1467676800000": {
"Close": 94.989998,
"High": 95.400002,
"Open": 95.389999,
"Adj Close": 94.989998,
"Volume": 27705200,
"Low": 94.459999
},
"1467763200000": {
"Close": 95.529999,
"High": 95.660004,
"Open": 94.599998,
"Adj Close": 95.529999,
"Volume": 30949100,
"Low": 94.370003
}
}
orient='records'(reset index in order to make column Date
visible):
orient='records'(重置索引以使列Date
可见):
In [99]: print(json.dumps(json.loads(apl.reset_index().to_json(orient='records')), indent=2))
[
{
"Close": 94.989998,
"High": 95.400002,
"Open": 95.389999,
"Adj Close": 94.989998,
"Volume": 27705200,
"Date": 1467676800000,
"Low": 94.459999
},
{
"Close": 95.529999,
"High": 95.660004,
"Open": 94.599998,
"Adj Close": 95.529999,
"Volume": 30949100,
"Date": 1467763200000,
"Low": 94.370003
},
{
"Close": 95.940002,
"High": 96.5,
"Open": 95.699997,
"Adj Close": 95.940002,
"Volume": 25139600,
"Date": 1467849600000,
"Low": 95.620003
}
]
you can make use of the following to_json()
parameters:
您可以使用以下to_json()
参数:
date_format: {‘epoch', ‘iso'}
Type of date conversion. epoch = epoch milliseconds, iso` = ISO8601, default is epoch.
date_unit: string, default ‘ms' (milliseconds)
The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s', ‘ms', ‘us', ‘ns' for second, millisecond, microsecond, and nanosecond respectively.
orient :string
The format of the JSON string
- split : dict like {index -> [index], columns -> [columns], data -> [values]}
- records : list like [{column -> value}, ... , {column -> value}]
- index : dict like {index -> {column -> value}}
- columns : dict like {column -> {index -> value}} values : just the values array
日期格式:{'纪元','iso'}
日期转换类型。epoch = 纪元毫秒,iso` = ISO8601,默认为纪元。
date_unit:字符串,默认'ms'(毫秒)
要编码到的时间单位,控制时间戳和 ISO8601 精度。's'、'ms'、'us'、'ns' 之一分别表示秒、毫秒、微秒和纳秒。
东方:字符串
JSON 字符串的格式
- split : dict like {index -> [index], columns -> [columns], data -> [values]}
- 记录:列表如 [{column -> value}, ... , {column -> value}]
- index : dict like {index -> {column -> value}}
- columns : dict like {column -> {index -> value}} values :只是 values 数组