json - 将嵌套 JSON 转换为 Pandas 列
问题描述
我有一个如下的 Json 文件:
[{
'instrument_token': 12335618, 'last_price': 31584.6,
'ohlc': {'open': 31080.1, 'high': 31590.0, 'low': 31049.05, 'close': 31114.7},
'depth': {'buy': [{'quantity': 40, 'price': 31576.4, 'orders': 1}, {'quantity': 160, 'price': 31576.0, 'orders': 1}], 'sell': [{'quantity': 200, 'price': 31584.6, 'orders': 2}, {'quantity': 60, 'price': 31584.65, 'orders': 1}]}
}]
我试过如下:
df = json_normalize(ticks)
print(df)
这给了我一个结果:
instrument_token last_price ohlc.open ohlc.high ohlc.low ohlc.close depth.buy depth.sell
0 12335618 31584.6 31080.1 31590.0 31049.05 31114.7 [{'quantity': 40, 'price': 31576.4, 'orders': ... [{'quantity': 200, 'price': 31584.6, 'orders':...
我想进一步规范化depth.buy & depth.sell列在单独列中的数据,列名如下:
depth.buy.quantity1, depth.buy.price1, depth.buy.orders1,
depth.buy.quantity2, depth.buy.price2, depth.buy.orders2,
depth.sell.quantity1, depth.sell.price1, depth.sell.orders1,
depth.sell.quantity2, depth.sell.price2, depth.sell.orders2,
是否可以进一步规范化?
解决方案
对于这个示例数据集,你可以这样做。
from pandas.io.json import json_normalize
data = [{
'instrument_token': 12335618, 'last_price': 31584.6,
'ohlc': {'open': 31080.1, 'high': 31590.0, 'low': 31049.05, 'close': 31114.7},
'depth': {'buy': [{'quantity': 40, 'price': 31576.4, 'orders': 1},
{'quantity': 160, 'price': 31576.0, 'orders': 1}],
'sell': [{'quantity': 200, 'price': 31584.6, 'orders': 2},
{'quantity': 60, 'price': 31584.65, 'orders': 1}]
}
}]
df = json_normalize(data)
cols = ["depth.buy","depth.sell"]
for c in cols:
postdf = pd.DataFrame()
tmp = df[c].values
for i,val in enumerate(tmp):
vals = list(val.values())
keys = [f"{c}.{k}{i+1}"for k in val.keys()]
tmpdf =pd.DataFrame([vals],columns=keys)
postdf = pd.concat([postdf,tmpdf],axis=1)
df = pd.concat([df,postdf],axis=1)
df = df.drop(columns=[c])
注意,
tmp = df[c].values
取列表的第 0 个元素。如果您有多个元素,则必须遍历元素。我假设一个列表中的所有数据。- 如果您需要动态获取列名列表 (["depth.buy","depth.sell"]),您可以通过检查
dtypes
并df
获取object
类型列名来实现。
推荐阅读
- typescript - @microsoft/ts-command-line:onDefineParameters 有什么用?
- python - selenium 可以在网站上发布真实流量吗?
- wso2 - 如何在具有 apache 前端的分布式设置中正确设置 APIM 存储?
- sql - 如何在sql server中根据条件生成序列号
- django - 如何自定义 ModelSerializer 的验证错误信息?
- office365 - O365 下的 Office 2016 和 Office 2016 是否使用相同的互操作 DLL
- google-chrome - 是否有 shim 或 polyfill 可以解决 Chrome 对数据列表的 512 限制?
- java - 如何迭代并获取动态传递的类的值
- javascript - JavaScript 错误:未捕获的类型错误:无法读取未定义的属性“删除”
- android-studio - 如何指定设备在android studio中运行应用程序