简体   繁体   English

在 Python Pandas 中读取 JSON 文件

[英]Read JSON file in Python Pandas

I have the following part of a JSON file:我有一个 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"}]

I want to read this in Python Pandas as a dataframe with the 4 columns as headers:我想在 Python Pandas 中将此作为数据框读取,其中 4 列作为标题:

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

I tried the following:我尝试了以下方法:

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

Which gives me a [1 rows x 235 columns] for the whole dataset.这给了我整个数据集的 [1 行 x 235 列]。 I want to get 4 columns dataframe.我想获得 4 列数据框。 How do I do this?我该怎么做呢?

you may try this approach.你可以试试这个方法。

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

output for the final dataframe will be one that consists of a header row, and two observations.最终数据帧的输出将是一个由标题行和两个观察值组成的数据帧。

我刚刚做了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM