【发布时间】:2022-01-05 02:02:16
【问题描述】:
这听起来是一个简单的问题,但我没有在 json 文件中得到我想要的输出。
从异常检测器获得结果后,我正在尝试将我的数据帧转换为 json。
数据框包含以下列,包括索引列作为日期时间:
index column - datetime,
loss,
rolling_mean,
exponential_mean
static_threshold,
dynamic_threshold,
anomlay,
global anomaly,
total value
我正在尝试使用 to_json 将这些数据框列转换为 json 文件。但是虽然我使用了 orient,但它并没有给我在 json 文件中的索引列。
我的代码:
#anomalies is a dataframe which has all the above columns
result = anomalies.to_json(orient='index')
parsed = json.loads(result)
json.dumps(parsed, indent=4)
print(parsed)
#print(result)
输出
{'1637593200000': {'loss': 0.2777269163,
'rolling_mean': 0.3497074445,
'exp_mean': 0.2994830801,
'static_threshold': 0.2707094526,
'dynamic_threshold': 0.3497074445,
'global_anomaly': False,
'anomaly': True,
'total': 0.2090592334},
'1637596800000': {'loss': 0.2816397219,
'rolling_mean': 0.3550902968,
'exp_mean': 0.3079049915,
'static_threshold': 0.2707094526,
'dynamic_threshold': 0.3550902968,
'global_anomaly': False,
'anomaly': True,
'total': 0.0818815331}}
我想要的是:
[{'datetime':20220105,
'loss': 0.2777269163,
'rolling_mean': 0.3497074445,
'exp_mean': 0.2994830801,
'static_threshold': 0.2707094526,
'dynamic_threshold': 0.3497074445,
'global_anomaly': False,
'anomaly': True,
'total': 0.2090592334},
{'datetime':20220105,
'loss': 0.2816397219,
'rolling_mean': 0.3550902968,
'exp_mean': 0.3079049915,
'static_threshold': 0.2707094526,
'dynamic_threshold': 0.3550902968,
'global_anomaly': False,
'anomaly': True,
'total': 0.0818815331}]
【问题讨论】:
标签: python json python-3.x dataframe