JSON对象中的Pandas数据框

3okqufwl  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(152)

我尝试从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"
              }
           ]
        }
     ]
      }
   ]
}

我只需要values,我需要将其转换为DataFrame,如下图所示。

nukf8bse

nukf8bse1#

尝试从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

即数据集的表

w8f9ii69

w8f9ii692#

有一个专门的panda函数pd.json_normalize()可以把json数据转换成一个平面表,因为要转换成dataframe的数据是嵌套在多个键下的,所以我们可以把它的路径作为一个列表传递,比如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)

相关问题