python - 使用 python pandas 读取多个嵌套的 json 文件
问题描述
我是python的初学者。我想像附件中一样阅读这个 json 文件 data1。我试图读取文件中的所有列,但我只能读取“数据”嵌套。我不知道如何读取“数据”和“引号”嵌套中的所有列。你能帮我么?谢谢
我的代码:
import pandas as pd
data = json.load(open('C:/JSON_IMPORT/data1.json'))
df = pd.DataFrame(data["data"])
print (df)
**Json file:**
```{"status":
{"timestamp":"2021-03-16T19:27:55.404Z","error_code":0,"error_message":null,"elapsed":173,"credit_count":22,"notice":null,"total_count":4368},
"data":[{"id":1,
"name":"Bitcoin",
"symbol":"BTC",
"slug":"bitcoin",
"num_market_pairs":9862,
"date_added":"2013-04-28T00:00:00.000Z",
"tags":["mineable","pow","sha-256","store-of-value","state-channels","coinbase-ventures-portfolio","three-arrows-capital-portfolio","polychain-capital-portfolio"],
"max_supply":21000000,
"circulating_supply":18655725,
"total_supply":18655725,
"platform":null,
"cmc_rank":1,
"last_updated":"2021-03-16T19:26:11.000Z",
"quote":{
"USD":{
"price":55643.86231386882,
"volume_24h":57006039705.56386,
"percent_change_1h":-0.22948654,
"percent_change_24h":-0.66133846,
"percent_change_7d":3.26713607,
"percent_change_30d":14.24843475,
"percent_change_60d":54.21680422,
"percent_change_90d":168.83609047,
"market_cap":1038076593265.4004,
"last_updated":"2021-03-16T19:26:11.000Z"}}
}}
]
解决方案
干得好。您应该使用pd.json_normalize
并将其与由data['status']
df = pd.concat([pd.DataFrame(data['status'],index=[0]),
pd.json_normalize(data, record_path=['data'])],
axis=1)
print(df)
# > timestamp error_code error_message elapsed credit_count notice total_count id name symbol slug num_market_pairs date_added tags max_supply circulating_supply total_supply platform cmc_rank last_updated quote.USD.price quote.USD.volume_24h quote.USD.percent_change_1h quote.USD.percent_change_24h quote.USD.percent_change_7d quote.USD.percent_change_30d quote.USD.percent_change_60d quote.USD.percent_change_90d quote.USD.market_cap quote.USD.last_updated
0 2021-03-16T19:27:55.404Z 0 null 173 22 null 4368 1 Bitcoin BTC bitcoin 9862 2013-04-28T00:00:00.000Z ['mineable', 'pow', 'sha-256', 'store-of-value', 'state-channels', 'coinbase-ventures-portfolio', 'three-arrows-capital-portfolio', 'polychain-capital-portfolio'] 21000000 18655725 18655725 null 1 2021-03-16T19:26:11.000Z 55643.86231386882 57006039705.56386 -0.22948654 -0.66133846 3.26713607 14.24843475 54.21680422 168.83609047 1038076593265.4004 2021-03-16T19:26:11.000Z
推荐阅读
- c# - DataGridview 值未清除并添加到新插入的值
- spring-boot - Spring Boot 应用程序在升级到 Apache Derby 10.15 后中断——为什么?
- tfs - 将 TFS 2012 项目集合移动到 Azure DevOps 2019
- python - Django IntegretyError,当尝试更改 ManyToMany 字段的值时
- angular - 将字符串转换为数字并在响应数组中的对象内将其倍增 - Angular 7
- oracle - Oracle 将 dmp 还原到现有数据库
- r - 使用 SLURM 数组将 R 脚本划分为子作业?
- r - 如何通过附加不同长度的字符串来消除重复字符串的歧义?
- java - 使用 appache common-codec library 获取无效数据解码
- javascript - 如何在不使用 z-index 的情况下使用 css 网格反向堆叠元素?