您可以使用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
我會爲了使用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 ='記錄'(爲了使柱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
}
]
,您可以利用以下to_json()
參數:
DATE_FORMAT:{' 時代」, '異'}
日期轉換的類型。 epoch = epoch毫秒,iso` = ISO8601,默認是epoch。
date_unit相同:串,默認「毫秒」(毫秒)
時間單位進行編碼來,治時間戳和ISO8601精度。 分別爲秒,毫秒,微秒和 納秒的「s」,「ms」,「us」,「ns」之一。
定向:串
JSON字符串的格式
- 分裂:字典等{索引 - > [指數],列 - > [列],數據 - > [數值] }
- 記錄:列表等[{柱 - >值},...,{柱 - >值}]
- 指數:DICT像{索引 - > {柱 - >值}}
- 列:字典像{c olumn - > {index - > value}} values:只是values數組