展平从 Tick Data 收集的嵌套字典,用于 2 个不同 'instrument_token'
Flatten a Nested Dictionary collected from Tick Data for 2 different 'instrument_token'
我收到了如下报价数据:
它嵌套了字典 'olhc' 和 'depth'。 同样,'depth' 嵌套为 'buy' 和 'sell'。这里我面临更多的问题。
[
{'tradable': True, 'mode': 'full', 'instrument_token': 20471298,
'last_price': 65.0, 'last_quantity': 1500, 'average_price': 66.0, 'volume':
3000, 'buy_quantity': 51000, 'sell_quantity': 54000, 'ohlc': {'open': 67.0,
'high': 67.0, 'low': 65.0, 'close': 58.8}, 'change': 10.544217687074834,
'last_trade_time': datetime.datetime(2018, 10, 22, 10, 46, 28), 'oi': 52500,
'oi_day_high': 55500, 'oi_day_low': 52500, 'timestamp':
datetime.datetime(2018, 10, 22, 15, 39, 6), 'depth': {'buy': [{'quantity':
0, 'price': 0.0, 'orders': 1}, {'quantity': 1500, 'price': 60.0, 'orders':
1}, {'quantity': 1500, 'price': 21.0, 'orders': 1}, {'quantity': 1500,
'price': 20.0, 'orders': 1}, {'quantity': 1500, 'price': 5.4, 'orders': 1}],
'sell': [{'quantity': 0, 'price': 0.0, 'orders': 1}, {'quantity': 3000,
'price': 97.0, 'orders': 1}, {'quantity': 1500, 'price': 121.0, 'orders':
1}, {'quantity': 48000, 'price': 144.3, 'orders': 1}, {'quantity': 0,
'price': 0.0, 'orders': 0}]}},
{'tradable': True, 'mode': 'full', 'instrument_token': 11955714,
'last_price': 70.5, 'last_quantity': 1500, 'average_price': 69.36, 'volume':
24000, 'buy_quantity': 45000, 'sell_quantity': 64500, 'ohlc': {'open': 55.0,
'high': 74.45, 'low': 54.0, 'close': 51.8}, 'change': 36.1003861003861,
'last_trade_time': datetime.datetime(2018, 10, 22, 15, 20, 35), 'oi':
166500, 'oi_day_high': 187500, 'oi_day_low': 166500, 'timestamp':
datetime.datetime(2018, 10, 22, 15, 34, 36), 'depth': {'buy': [{'quantity':
1500, 'price': 71.35, 'orders': 1}, {'quantity': 1500, 'price': 69.3,
'orders': 1}, {'quantity': 1500, 'price': 69.1, 'orders': 1}, {'quantity':
1500, 'price': 68.05, 'orders': 1}, {'quantity': 1500, 'price': 68.0,
'orders': 1}], 'sell': [{'quantity': 1500, 'price': 78.0, 'orders': 1},
{'quantity': 1500, 'price': 79.0, 'orders': 1}, {'quantity': 1500, 'price':
80.0, 'orders': 1}, {'quantity': 1500, 'price': 90.0, 'orders': 1},
{'quantity': 58500, 'price': 105.6, 'orders': 1}]}},
]
我想展平整个数据并保存到 pandas 数据框中,所有字段都在单独的列中。
请帮忙。
pandas.io.json.normalize.json_normalize
in Pandas as at v0.23.4 不支持规范化共享相同 root[= 的记录路径28=].
需要对同一数据分别对此类对象进行规范化。可以使用 pandas.core.reshape.concat.concat
.
连接生成的数据框对象
首先规范化其中一个嵌套的买入或卖出列表,
buys_keypath = ['depth', 'buy']
df_from_buys_keypath = json_normalize(dct,
[['depth', 'buy']],
record_prefix='depth.buy.')
然后用另一个嵌套列表作为记录路径对其余数据进行归一化。
def keypaths(dct):
ret_list = []
for k in dct.keys():
if isinstance(dct[k], dict):
for keypath in keypaths(dct[k]):
keypaths_ = keypath if isinstance(keypath, list) else [keypath]
ret_list += [[k, *keypaths_]]
else:
ret_list += [k,]
return ret_list
rest_keypath = list(
filter(
lambda keypath: keypath != ['depth', 'buy'], # filter out buys_keypath
keypaths(dct[0])
)
)
df_from_rest_keypath = json_normalize(dct,
[['depth', 'sell']],
rest_keypath,
record_prefix='depth.sell.')
最后,沿着它们的列连接两个数据框对象
df = pandas.concat([df_from_buys_keypath, df_from_other_keypath],
axis=1)
我收到了如下报价数据: 它嵌套了字典 'olhc' 和 'depth'。 同样,'depth' 嵌套为 'buy' 和 'sell'。这里我面临更多的问题。
[
{'tradable': True, 'mode': 'full', 'instrument_token': 20471298,
'last_price': 65.0, 'last_quantity': 1500, 'average_price': 66.0, 'volume':
3000, 'buy_quantity': 51000, 'sell_quantity': 54000, 'ohlc': {'open': 67.0,
'high': 67.0, 'low': 65.0, 'close': 58.8}, 'change': 10.544217687074834,
'last_trade_time': datetime.datetime(2018, 10, 22, 10, 46, 28), 'oi': 52500,
'oi_day_high': 55500, 'oi_day_low': 52500, 'timestamp':
datetime.datetime(2018, 10, 22, 15, 39, 6), 'depth': {'buy': [{'quantity':
0, 'price': 0.0, 'orders': 1}, {'quantity': 1500, 'price': 60.0, 'orders':
1}, {'quantity': 1500, 'price': 21.0, 'orders': 1}, {'quantity': 1500,
'price': 20.0, 'orders': 1}, {'quantity': 1500, 'price': 5.4, 'orders': 1}],
'sell': [{'quantity': 0, 'price': 0.0, 'orders': 1}, {'quantity': 3000,
'price': 97.0, 'orders': 1}, {'quantity': 1500, 'price': 121.0, 'orders':
1}, {'quantity': 48000, 'price': 144.3, 'orders': 1}, {'quantity': 0,
'price': 0.0, 'orders': 0}]}},
{'tradable': True, 'mode': 'full', 'instrument_token': 11955714,
'last_price': 70.5, 'last_quantity': 1500, 'average_price': 69.36, 'volume':
24000, 'buy_quantity': 45000, 'sell_quantity': 64500, 'ohlc': {'open': 55.0,
'high': 74.45, 'low': 54.0, 'close': 51.8}, 'change': 36.1003861003861,
'last_trade_time': datetime.datetime(2018, 10, 22, 15, 20, 35), 'oi':
166500, 'oi_day_high': 187500, 'oi_day_low': 166500, 'timestamp':
datetime.datetime(2018, 10, 22, 15, 34, 36), 'depth': {'buy': [{'quantity':
1500, 'price': 71.35, 'orders': 1}, {'quantity': 1500, 'price': 69.3,
'orders': 1}, {'quantity': 1500, 'price': 69.1, 'orders': 1}, {'quantity':
1500, 'price': 68.05, 'orders': 1}, {'quantity': 1500, 'price': 68.0,
'orders': 1}], 'sell': [{'quantity': 1500, 'price': 78.0, 'orders': 1},
{'quantity': 1500, 'price': 79.0, 'orders': 1}, {'quantity': 1500, 'price':
80.0, 'orders': 1}, {'quantity': 1500, 'price': 90.0, 'orders': 1},
{'quantity': 58500, 'price': 105.6, 'orders': 1}]}},
]
我想展平整个数据并保存到 pandas 数据框中,所有字段都在单独的列中。
请帮忙。
pandas.io.json.normalize.json_normalize
in Pandas as at v0.23.4 不支持规范化共享相同 root[= 的记录路径28=].
需要对同一数据分别对此类对象进行规范化。可以使用 pandas.core.reshape.concat.concat
.
首先规范化其中一个嵌套的买入或卖出列表,
buys_keypath = ['depth', 'buy']
df_from_buys_keypath = json_normalize(dct,
[['depth', 'buy']],
record_prefix='depth.buy.')
然后用另一个嵌套列表作为记录路径对其余数据进行归一化。
def keypaths(dct):
ret_list = []
for k in dct.keys():
if isinstance(dct[k], dict):
for keypath in keypaths(dct[k]):
keypaths_ = keypath if isinstance(keypath, list) else [keypath]
ret_list += [[k, *keypaths_]]
else:
ret_list += [k,]
return ret_list
rest_keypath = list(
filter(
lambda keypath: keypath != ['depth', 'buy'], # filter out buys_keypath
keypaths(dct[0])
)
)
df_from_rest_keypath = json_normalize(dct,
[['depth', 'sell']],
rest_keypath,
record_prefix='depth.sell.')
最后,沿着它们的列连接两个数据框对象
df = pandas.concat([df_from_buys_keypath, df_from_other_keypath],
axis=1)