python - 如何使用 Python 中 JSON 输出的数据创建表

标签 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"
              }
           ]
        }
     ]
      }
   ]
}

我只需要 ,我需要转换成如下图所示的数据框(点击时间序列数据链接)

Timeseries data

最佳答案

试试这个只拉出一个列表 values从你的 json

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

这是你的数据集表

关于python - 如何使用 Python 中 JSON 输出的数据创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862308/

相关文章:

python - Selenium Chromedriver 挂起?

javascript - 从 JavaScript 中的 JQuery.each() 返回一个对象

json - 如何为包含 Properties 对象的对象定义 JSON 模式?

javascript - 将平面 JSON 转换为父子关系结构

python - 尝试用Python中的正则表达式替换\t为\s,但结果出现 "Unhashable type:list"错误

python - 添加 2 个不同长度的列表列表

python - Sympy:使用 sympify 将自定义函数减少为字符串?

python - Selenium Python "Name Error"我肯定错过了一些明显的东西

python - JSON解码错误 : Unexpected UTF-8 BOM: Display problems in bash?

javascript - 如何将node.js传递到html?