python - 如何使用 pd normalize 展平嵌套的 json
问题描述
我正在使用 ES 进行聚合,结果如下:
{'took': 27,
'timed_out': False,
'_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 233, 'relation': 'eq'},
'max_score': None,
'hits': []},
'aggregations': {'sales_over_time': {'buckets': [{'key': 1617235200000,
'doc_count': 9,
'name': {'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{'key': '624232499',
'doc_count': 4,
'latest_comment': {'hits': {'total': {'value': 4, 'relation': 'eq'},
'max_score': None,
'hits': [{'_index': 'data',
'_type': 'test',
'_id': 'Hb5Uj3gBm2iwycZfdDvr',
'_score': None,
'_source': {'totalsales': 2630149, 'Id': '624232499'},
'sort': [1617312374760]}]}}},
{'key': '624232532',
'doc_count': 4,
'latest_comment': {'hits': {'total': {'value': 4, 'relation': 'eq'},
'max_score': None,
'hits': [{'_index': 'data',
'_type': 'test',
'_id': 'q77NjngBm2iwycZf6hdU',
'_score': None,
'_source': {'sales': 5810, 'Id': '624232532'},
'sort': [1617303556611]}]}}},
{'key': '656625970',
'doc_count': 1,
'latest_comment': {'hits': {'total': {'value': 1, 'relation': 'eq'},
'max_score': None,
'hits': [{'_index': 'data',
'_type': 'test',
'_id': 'Nb4xj3gBm2iwycZfFjKH',
'_score': None,
'_source': {'totalsales': 12690, 'Id': '656625970'},
'sort': [1617310056788]}]}}}]}},
我试图得到结果并使用 pd.normalze 像 test_json= pd.json_normalize(result['aggregations']['sales_over_time']['buckets']) 它出来了
key doc_count name.doc_count_error_upper_bound name.sum_other_doc_count name.buckets
0 1617235200000 9 0 0 [{'key': '624232499', 'doc_count': 4, 'latest_...
1 1617321600000 9 0 0 [{'key': '624232499', 'doc_count': 4, 'latest_.
所以我尝试使用扁平化 NESTED json
pd.json_normalize(result['aggregations']['sales_over_time']['buckets']).explode("name.buckets").to_dict(orient="records"))
并且新闻中有一个“name.buckets.latest_comment.hits.hits”的嵌套层
ey doc_count name.doc_count_error_upper_bound name.sum_other_doc_count name.buckets.key name.buckets.doc_count name.buckets.latest_comment.hits.total.value name.buckets.latest_comment.hits.total.relation name.buckets.latest_comment.hits.max_score name.buckets.latest_comment.hits.hits
0 1617235200000 9 0 0 624232499 4 4 eq None [{'_index': 'data', '_type': 'test', '_...
1 1617235200000 9 0 0 624232532 4 4 eq None [{'_index': 'data', '_type': 'test', '_...
我怎样才能扁平化所有嵌套的 json ?
解决方案
js
是您的示例 JSON- 四通关可以
json_normalize()
吗 explode()
嵌入式列表,reset_index()
如有必要
pd.json_normalize(pd.json_normalize(pd.json_normalize(pd.json_normalize(js)
.explode("aggregations.sales_over_time.buckets")
.to_dict(orient="records"))
.explode("aggregations.sales_over_time.buckets.name.buckets")
.reset_index(drop=True)
.to_dict(orient="records"))
.explode("aggregations.sales_over_time.buckets.name.buckets.latest_comment.hits.hits")
.to_dict(orient="records")
)
推荐阅读
- php - 当输出文件的内容和标题在波兰字符的位置时,我不断收到奇怪的字符
- javascript - imported json data assigning to existing state not working
- intellij-idea - Jetbrains Rider Plugin Trigger Build
- sql - 仅选择那些匹配或不存在于另一个表中的记录
- laravel - 路线的顺序影响操作
- html - 如何根据表头属性突出显示父表行
- ios - 如何以编程方式在导航控制器中嵌入视图?
- python - 为什么 tolist 转换后的数组不附加在 python 多重处理中?
- intellij-idea - Rider 插件中活动项目中的运行方法
- python - 如何将缓冲区字符串从node.js转换为python中的数组