为了获取OHLCV数据,我用熊猫对实时数据进行了重新采样:
message.guild
数据框如下所示:
ohlcv = df.resample(_period).agg({'bid': 'ohlc', 'volume': 'sum'})
,使用 volume bid
volume open high low close
timestamp
2016-09-01 300.0 77.644997 78.320331 77.638 78.320331
的JSON输出为:
ohlcv.to_json(orient='index')
如何在以下JSON中转换数据框:
{"1472688000000":{"["volume","volume"]":300.0,"["bid","open"]":77.644997,"["bid","high"]":78.320331,"["bid","low"]":77.638,"["bid","close"]":78.320331}}
答案 0 :(得分:1)
使用MultiIndex.droplevel
将列中的MultiIndex
转换为展平列:
ohlcv = df.resample(_period).agg({'bid': 'ohlc', 'volume': 'sum'})
ohlcv.columns = ohlcv.columns.droplevel(0)
ohlcv.to_json(orient='index')