熊猫OHLCV到JSON格式

时间:2018-08-28 08:56:54

标签: python json pandas resampling

为了获取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}}

1 个答案:

答案 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')