首页 > 解决方案 > 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

谢谢

标签: pythonjsonpandas

解决方案


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

推荐阅读