如何在数据框中加载此JSON?

时间:2019-02-24 18:20:15

标签: python json python-2.7 dataframe

尝试在Python中读取以下JSON文件:

[[ {"fecha" : "2013-01-01","indicativo" : "3195","nombre" : "MADRID, RETIRO","provincia" : "MADRID","altitud" : "667","tmed" : "6,4","prec" :"0,0"},{"fecha" : "2013-01-02","indicativo" : "3196","nombre" : "MADRID,RETIRO","provincia" : "MADRID","altitud" : "667","tmed" : "6,5","prec" : "0,0"},{"fecha" : "2013-01-03","indicativo" : "3197","nombre": "MADRID, RETIRO","provincia" : "MADRID","altitud" : "667","tmed" : "6,7","prec" : "0,0"}],[ {"fecha" : "2013-02-01","indicativo" : "3195","nombre" : "MADRID, RETIRO","provincia" : "MADRID","altitud" : "667","tmed" : "6,4","prec" : "0,0"},{"fecha" : "2013-02-02","indicativo" :"3196","nombre" : "MADRID, RETIRO","provincia" : "MADRID","altitud" : "667","tmed" : "6,5","prec" : "0,0"}]]

我尝试了以下选项:

选项1:

df2 = pd.read_json('test.json',orient='split')

结果:

AttributeError: 'list' object has no attribute 'iteritems'

选项2:

df2 = pd.read_json('test.json',orient='columns')

结果: result 2

我想要的预期结果是:

enter image description here

基本上是一个扁平化的数据框,每个元素的名称为列名=属性,行为行。

2 个答案:

答案 0 :(得分:0)

也许您可以尝试这段代码:

import pandas as pd
import json


file = './test.json'

with open(file, 'r') as f:
    data = json.load(f)


big_df = pd.DataFrame()

for i in range(len(data)):
    a = json.dumps(data[i])
    big_df = pd.concat([big_df, pd.read_json(a)], axis=0)

big_df.reset_index(drop=True)

注意事项:我已经测试了python3中的代码,但我认为它也应该在python2中工作。

答案 1 :(得分:0)

pd.DataFrame构造函数可以将字典列表作为输入,因此解决方案很简单:

import pandas as pd    
import json

file = 'test.json'
with open(file, 'r') as f:
    data = json.load(f)
rows = data[0]
df = pd.DataFrame(rows)

如果您想知道pd.read_json()输入的正确格式,请查看df.to_json()。