从JSON对象创建Pandas DataFrame

12
我试图从下面所示的JSON输出中创建一个DataFrame。
{  
   "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"
              }
           ]
        }
     ]
      }
   ]
}

我只需要,并且需要将其转换为下面图片中所示的DataFrame。

时间序列数据

2个回答

18

尝试这个方法,从你的json中只提取出一个列表


注:为了保留原文意思和html标签,我将"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

你的数据集对应哪张表格


0

有一个专门的pandas函数pd.json_normalize(),可以将json数据转换为平面表格。由于要转换为数据框的数据嵌套在多个键下面,因此我们可以将其路径作为列表传递给record_path= kwarg。 values的路径是tags -> results -> values,因此我们将其作为列表传递。

# first load the json file
import json
with open(file_path, 'r') as f:
    data = json.load(f)

# convert `data` into a dataframe
df = pd.json_normalize(data, record_path=['tags', 'results', 'values']).set_axis(['time', 'temperature', 'quality'], axis=1)

res


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接