[英]read multiple nested json file with python pandas
im beginner with python.我是 python 的初学者。 i want to read this json file data1 like in the attachment.
我想像附件中一样阅读这个 json 文件 data1。 i have tried to read all columns in the file, but i can only read the 'data' nest.
我试图读取文件中的所有列,但我只能读取“数据”嵌套。 i don't know how to read all the columns in both "data" and "quotes" nest.
我不知道如何读取“数据”和“引号”嵌套中的所有列。 can you please help me?
你能帮我么? Thankyou
谢谢
my code:我的代码:
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"}}
}}
]
Here you go.这里是 go。 You should use
pd.json_normalize
and concatenate that with the dataframe made from data['status']
您应该使用
pd.json_normalize
并将其与由data['status']
制成的 dataframe 连接
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.