# transforms datetime into timestamp in secondst = df.index.values.astype(np.int64) // 10**6return Jsonify(np.c_[t,df.open,df.high,df.low,df.close,df.volume].toList())
其中df是一个包含索引的数据框,该索引是一个日期,并且至少(但不仅仅是)以下属性:open,high,low,close,volume.然后我使用flask的Jsonify将新创建的数组输出为JsON.上面的代码可以工作,但对于我如何使其更好/更高效的任何想法,它看起来非常低效.
解决方法 你可以使用 to_json()方法:In [88]: import pandas_datareader.data as webIn [89]: apl = web.get_data_yahoo('AAPL','2016-07-05','2016-07-07')In [90]: aplOut[90]: Open High Low Close Volume Adj CloseDate2016-07-05 95.389999 95.400002 94.459999 94.989998 27705200 94.9899982016-07-06 94.599998 95.660004 94.370003 95.529999 30949100 95.5299992016-07-07 95.699997 96.500000 95.620003 95.940002 25139600 95.940002
我将使用Json.dumps(…,indent = 2)以使其更好/可读:
In [91]: import Json
东方=“索引”
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'(重置索引以使列Date可见):
In [99]: print(Json.dumps(Json.loads(apl.reset_index().to_Json(orIEnt='records')),indent=2))[ { "Close": 94.989998,"Date": 1467676800000,{ "Close": 95.529999,"Date": 1467763200000,"Low": 94.370003 },{ "Close": 95.940002,"Date": 1467849600000,"Low": 95.620003 }]
您可以使用以下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
以上是内存溢出为你收集整理的python – 将数据帧输出到json数组全部内容,希望文章能够帮你解决python – 将数据帧输出到json数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)