我正在尝试从文本文件读取到熊猫数据框。文本文件似乎是JSON的2D数组,我该如何读取?
[[{{'metric_name':'CPU','category':'A','data':'9','time_stamp':'2019-03-28 13:15:31'}],[ {'metric_name':'Disk','category':'B','data':'56','time_stamp':'2019-03-28 13:15:31'}]]
我希望将参数“ metric_name”,“ category”,“ data”,“ time_stamp”作为标题
答案 0 :(得分:0)
这是一个解决方案:
import json
import pandas as pd
# load the file
raw_data = json.load(open('myfile.json'))
# raw_data contains a nested list, so convert it to a simple list :
data = [x[0] for x in raw_data]
# then create the dataframe
df = pd.DataFrame.from_records(data)
这里是data
的内容。嵌套列表已转换为简单列表(假设每个数组有一条记录):
[{"category": "VM1",
"data": "9",
"metric_name": "CPU",
"time_stamp": "2019-03-28 13:15:31"},
{"category": "VM1",
"data": "9",
"metric_name": "CPU",
"time_stamp": "2019-03-28 13:15:31"}]