python - 将 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中的数据框吗?
解决方案
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
推荐阅读
- sql - 查询营业年度之间的记录
- android - Shrinker Config 文件中的 ProGuard 未解析引用
- c# - VisualStateManager.GoToState 不适用于 TextBox (UWP)
- c++ - 哪种方法最适合使用 Vulkan API 实现粒子着色器?
- vba - export an image from Powerpoint
- javascript - 如何设置在 ng-select angular 8 上选择的第一个选项?
- java - Android Studio : 无法下载 FastInfoset.jar (com.sun.xml.fastinfoset:FastInfoset:1.2.15): No cached version available for offline mode 错误
- r - R中的回归
- api - IIS API 版本控制
- google-analytics - 重定向后的 GA 目标转换