为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python Pandas 中读取 JSON 文件

在 Python Pandas 中读取 JSON 文件

PHP
慕哥9229398 2023-11-09 21:08:42
我有 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 列数据框。我该怎么做呢?
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

你可以尝试这种方法。


import pandas as pd

dataframe = pd.read_json('sample_json_file.json', orient='values')

print(dataframe)

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


查看完整回答
反对 回复 2023-11-09
?
慕神8447489

TA贡献1780条经验 获得超1个赞

我刚刚这样做:

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


查看完整回答
反对 回复 2023-11-09
  • 2 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信