在python中读取JSON文件并将其转换为Pandas DataFrame

时间:2019-11-06 17:53:02

标签: python json pandas dataframe

JSON文件中的数据采用以下格式

{"data":{"process1":[{"time":"2012-01-02T20:13:56+00:00" , "value":"400.63"}, {"time":"2013-04-02T22:13:56+00:00" , "value":"800.43"}, {"time":"2013-06-02T21:13:56+00:00" , "value":"600.43"},{"time":"2014-07-02T21:13:56+00:00" , "value":"100.43"}],
"process2":[{"time":"2012-02-02T20:13:56+00:00" , "value":"50.63"}, {"time":"2013-05-06T22:13:56+00:00" , "value":"400.43"}]}}

请注意,process1(4)和process2(2)中的键,值对的数量不同 我想将以上数据转换为以下Python Pandas数据框格式 Dataframe format

1 个答案:

答案 0 :(得分:0)

使用pd.concat + DataFrame.add_prefix

json={"data":{"process1":[{"time":"2012-01-02T20:13:56+00:00" , "value":"400.63"}, {"time":"2013-04-02T22:13:56+00:00" , "value":"800.43"}, {"time":"2013-06-02T21:13:56+00:00" , "value":"600.43"},{"time":"2014-07-02T21:13:56+00:00" , "value":"100.43"}],
"process2":[{"time":"2012-02-02T20:13:56+00:00" , "value":"50.63"}, {"time":"2013-05-06T22:13:56+00:00" , "value":"400.43"}]}}

new_df=pd.concat([pd.DataFrame(y).add_prefix(f'{x}_') for x,y in json['data'].items()],axis=1)
print(new_df)

输出

               process1_time process1_value              process2_time  \
0  2012-01-02T20:13:56+00:00         400.63  2012-02-02T20:13:56+00:00   
1  2013-04-02T22:13:56+00:00         800.43  2013-05-06T22:13:56+00:00   
2  2013-06-02T21:13:56+00:00         600.43                        NaN   
3  2014-07-02T21:13:56+00:00         100.43                        NaN   

  process2_value  
0          50.63  
1         400.43  
2            NaN  
3            NaN