【问题标题】:Read JSON file in Python Pandas在 Python Pandas 中读取 JSON 文件
【发布时间】:2020-10-14 07:49:34
【问题描述】:

我有一个 JSON 文件的以下部分:

[{"Date":"2020-02-17","Rt_low":0.5,"Rt_avg":1.93,"Rt_up":4,"population":"hosp"},{"Date":"2020-02-18","Rt_low":0,"Rt_avg":1.74,"Rt_up":4,"population":"hosp"}]

我想在 Python Pandas 中将其作为数据框读取,其中 4 列作为标题:

L = ['Date','Rt_low','Rt_up','population']

我尝试了以下方法:

df = pd.DataFrame(pd.read_json(file_name, lines=True))

这为我提供了整个数据集的 [1 行 x 235 列]。我想获得 4 列数据框。我该怎么做?

【问题讨论】:

    标签: python json pandas dataframe


    【解决方案1】:

    你可以试试这个方法。

    import pandas as pd
    dataframe = pd.read_json('sample_json_file.json', orient='values')
    print(dataframe)
    

    最终数据帧的输出将由标题行和两个观察值组成。

    【讨论】:

      【解决方案2】:

      我刚刚做到了:

      df = pd.DataFrame(pd.read_json(file_name))
      

      【讨论】:

        猜你喜欢
        • 2018-10-14
        • 1970-01-01
        • 1970-01-01
        • 2017-10-03
        • 1970-01-01
        • 2021-12-08
        • 2017-12-14
        • 2021-10-21
        相关资源
        最近更新 更多