【发布时间】:2020-11-12 03:47:53
【问题描述】:
我有以下嵌套的 json:
import requests
headers = {
'Content-Type': 'application/json'
}
r = requests.get("...API DATA...")
[
{
"ticker":"btcusd",
"baseCurrency":"btc",
"quoteCurrency":"usd",
"priceData":[
{
"open":3914.749407813885,
"high":3942.374263716895,
"low":3846.1755315352952,
"close":3849.1217299601617,
"date":"2019-01-02T00:00:00+00:00",
"tradesDone":756.0,
"volume":339.68131616889997,
"volumeNotional":1307474.735327181
}
]
}
]
我想把它转换成 Pandas 数据框。我已经能够做到这一点:
j = r.json()
df = pd.DataFrame.from_dict(j)
df
输出:
我还想在列中展开“priceData”。
我尝试了不同的方法,包括json.normalise和json.loads,但总是出现我无法理解的错误。
谁能告诉我怎么做才能理解?
谢谢!
编辑
priceData 包含超过 1 个元素。
“ticker”、“baseCurrency”和“quoteCurrency”在数据帧中并不是必需的,因此可以丢弃。
【问题讨论】:
-
能否为 priceData 添加更多详细信息?它总是列出 1 个元素吗?那个json呢?它总是列出 1 个元素吗?
-
priceData 有超过 1 个元素。 'Ticker'、'baseCurrency' 和 'quoteCurrency' 根本不需要,可能会被丢弃。谢谢@SatrioAdiPrabowo t
标签: python json pandas dataframe nested