如何使用Python中的JSON输出创建包含数据的表

时间:2016-04-26 10:30:22

标签: python json

我是Python的新手,所以我尝试从我的JSON OUTPUT

创建一个数据框

我的json输出如下所示

{  
   "tags":[  
      {  
     "stats":{  
        "rawCount":9
     },
     "name":"Temperature1",
     "results":[  
        {  
           "attributes":{  
              "Location":[  
                 "3rd Floor"
              ],
              "Sensor-Serial-Number":[  
                 "PT100"
              ]
           },
           "values":[  
              [  
                 1460958592800,
                 24.2,
                 3
              ],
              [  
                 1460958602800,
                 24.1,
                 1
              ],
              [  
                 1460958612800,
                 23.9,
                 1
              ],
              [  
                 1460958622800,
                 24.2,
                 1
              ],
              [  
                 1460958632800,
                 24.5,
                 1
              ],
              [  
                 1460958642800,
                 24.9,
                 1
              ],
              [  
                 1460958652800,
                 24.6,
                 1
              ],
              [  
                 1460958662800,
                 24.7,
                 1
              ],
              [  
                 1460958672800,
                 24.7,
                 1
              ]
           ],
           "groups":[  
              {  
                 "type":"number",
                 "name":"type"
              }
           ]
        }
     ]
      }
   ]
}

我只需要,我需要转换为数据帧,如下面PIC所示(点击时间序列数据链接)

Timeseries data

1 个答案:

答案 0 :(得分:5)

尝试从json

中仅提取values列表
import json
import ast
import pandas as pd
mystr = """
{'tags': [{'name': 'Temperature1',
  'results': [{'attributes': {'Location': ['3rd Floor'],
  'Sensor-Serial-Number': ['PT100']},
  'groups': [{'name': 'type', 'type': 'number'}],
  'values': [[1460958592800, 24.2, 3],
  [1460958602800, 24.1, 1],
  [1460958612800, 23.9, 1],
  [1460958622800, 24.2, 1],
  [1460958632800, 24.5, 1],
  [1460958642800, 24.9, 1],
  [1460958652800, 24.6, 1],
  [1460958662800, 24.7, 1],
  [1460958672800, 24.7, 1]]}],
 'stats': {'rawCount': 9}}]}
"""
val = ast.literal_eval(mystr)
val1 = json.loads(json.dumps(val))
val2 = val1['tags'][0]['results'][0]['values']
print pd.DataFrame(val2, columns=["time", "temperature", "quality"])

结果证明是

            time  temperature  quality
0  1460958592800         24.2        3
1  1460958602800         24.1        1
2  1460958612800         23.9        1
3  1460958622800         24.2        1
4  1460958632800         24.5        1
5  1460958642800         24.9        1
6  1460958652800         24.6        1
7  1460958662800         24.7        1
8  1460958672800         24.7        1

这是您的数据集表