python - Python 将 Json 读入 DataFrame
问题描述
假设我有以下 json 数据:
[
{
"cust_Name": [
"Nia Bernard",
"Nicole",
"Katelin"
],
"cust_Rate": [
"1.0 out of 5 stars",
"4.0 out of 5 stars",
"5.0 out of 5 stars"
],
"date_comment": [
"Reviewed in the United States on January 10, 2019",
"Reviewed in the United States on December 18, 2018",
"Reviewed in the United States on August 14, 2017"
]
}
]
如何使用以下输出将此 json 格式读入数据帧:
cust_Name cust_Rate date_comment
Nia Bernard 1.0 out of 5 stars Reviewed in the United States on January 10, 2019
Nicole 4.0 out of 5 stars Reviewed in the United States on December 18, 2018
Katelin 5.0 out of 5 stars Reviewed in the United States on August 14, 2017
谢谢
解决方案
data=[
{
"cust_Name": [
"Nia Bernard",
"Nicole",
"Katelin"
],
"cust_Rate": [
"1.0 out of 5 stars",
"4.0 out of 5 stars",
"5.0 out of 5 stars"
],
"date_comment": [
"Reviewed in the United States on January 10, 2019",
"Reviewed in the United States on December 18, 2018",
"Reviewed in the United States on August 14, 2017"
]
}
]
现在只需使用以下Dataframe()
方法创建数据框pandas
:-
import pandas as pd
df=pd.DataFrame(data[0])
现在,如果您打印df
,您将获得预期的输出:-
cust_Name cust_Rate date_comment
0 Nia Bernard 1.0 out of 5 stars Reviewed in the United States on January 10, 2019
1 Nicole 4.0 out of 5 stars Reviewed in the United States on December 18, ...
2 Katelin 5.0 out of 5 stars Reviewed in the United States on August 14, 2017
推荐阅读
- php - Laravel,身份验证控制器不存在于 app/http/controllers 或任何地方
- ruby - 如何修复随机冻结的字符串
- mongodb - MongoDB,如何将查找和排序与聚合中的 $cond 结合起来?
- python - boto3`get_object()`和`download_file()`有什么区别?
- scripting - 当我们尝试编写整个数据库的脚本时,SSMS 18.4 崩溃
- json - 如何从超 JSON 主体中拆分数据,以便拥有一个包含多个键和值的哈希图?
- reactjs - 无法创建反应项目
- c# - 从 Chrome 80+ 上的支付网关回发时,在 ASP.net Global.asax 中触发的请求和事件出现问题
- java - java中不同http客户端超时时间单位的意义
- reactjs - 使用 Etsy API 的 CORS 和 ReactJS