首页 > 解决方案 > 将 Json 从 api 代码转换为数据帧

问题描述

我是 python 新手,我从网站上提取了一些评论,我使用 api 将我的数据导入 python,格式为 json。我想将此json转换为python中的数据框。有人可以指导我如何执行此操作。

下面是导入json格式的api提取的代码

import requests

params = {
  "api_key": "abc",
  "format": "json"
}
r = requests.get('https://www.parsehub.com/api/v2/runs/ttx8PT-EL6Rf/data', params=params)
print(r.text)

我的输出如下:

{
     "name": "136",
     "Date": "September 07, 2019",
     "comment": "enjoy a lovely moment"
    },
    {
     "name": "135",
     "Date": "July 15, 2019",
     "comment": "I was there for my honeymoon. The hotel was simply wooww and wonderful. ALL the hotel staff was extremely friendly and made us felt at home. Deluxe Room very nice. Spa, hammam sauna very good. I indeed appreciated the hotel, without forgetting its meals, simply amazing quality and variety, i ate a lot. Relaxing moments with birds chirping, different swings to chill. Overall, I shall visit again. Thanks Azuri & Marideal."
    },
    {
     "name": "134",
     "Date": "June 12, 2019",
     "comment": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
    },

它继续。你能帮我把它转换成python中的数据框吗?

标签: pythonjson

解决方案


import pandas as pd
import simplejson as json  

data = json.dumps(r) #convert to json
data = json.loads(data)
dataFrame = pd.DataFrame.from_dict(data) #convert json to dataframe  

推荐阅读