如何将返回的JSON数据从API转换为DataFrame

时间:2019-03-14 01:54:09

标签: python-3.x pandas dataframe

我正在尝试使用Pandas将从API返回的原始JSON数据转换为DataFrame,但是我没有任何运气。这是我使用请求模块获取的返回的JSON数据:

{
"id": "1MUB48cgb5uy54V_ErhMQzbu-wTRNa-9EVgDbbL4u_30sdQ",
"accountId": "rTtNLALKTnMMbO_BYWRZks9JVS51o1TvSRU3-YQDPlkRsZY",
"puuid": "Ymwm3n3wArC94OnrPDwDuK5nb98fhL4-usc3lZ14UdlbrZqOUHVWmHSh42W6-ICFAIM0LEUYT0RhsA",
"name": "A Catalyst",
"profileIconId": 7,
"revisionDate": 1552276172000,
"summonerLevel": 177
}

现在,我们将上述数据存储在变量“ r”中。我以为它就像

一样简单
import pandas as pd

dataFrame = pd.DataFrame(r)
print(dataFrame)

但是情况并非如此,因为我收到错误消息:

ValueError: If using all scalar values, you must pass an index

1 个答案:

答案 0 :(得分:0)

import pandas as pd

dataFrame = pd.DataFrame.from_records(r)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_records.html