這聽起來很簡單,但我沒有在 json 檔案中得到我想要的輸出。
從例外檢測器獲得結果后,我試圖將我的資料幀轉換為 json。
資料框包含以下列,包括索引 coulumn 作為日期時間。
index column - datetime,
loss,
rolling_mean,
exponential_mean
static_threshold,
dynamic_threshold,
anomlay,
global anomaly,
total value
我正在嘗試通過使用將這些資料框列轉換為 json 檔案to_json。但它沒有給我 json 檔案中的索引列,盡管我使用了orient.
我的代碼:
#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}]
uj5u.com熱心網友回復:
pandas DataFrame 有一個函式可以轉換index為column. 它是.reset_index()。
如果將此函式與orient='records'in 一起使用.to_json(),則可以解決它。
這是我的解決方案。
result = anomalies.reset_index().to_json(orient='records', indent=4)
print(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403756.html
標籤:
