我想將資料框轉換為 json 檔案。資料框的其中一列包含時間序列作為字串。因此,最終的 json 如下所示:
[{"...":"...","Dauer":"24h","Wertereihe":"8619.0,9130.0,8302.0,8140.0"}, {...}, {...}]
是否可以以“Wertereihe”是一個數字陣列的方式將 df 保存到 json 檔案中?這將給出:[{"...":"...","Dauer":"24h","Wertereihe":[8619.0,9130.0,8302.0,8140.0]}, {...}, {...}]
我使用以下代碼段將 df 保存到 json 檔案:
df.to_json(jsonFile, orient = "records")
uj5u.com熱心網友回復:
IIUC,你需要:
df['Wertereihe'] = df['Wertereihe'].apply(lambda x: list(map(float, x.split(','))))
df.to_json(jsonFile, orient = "records")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329061.html
標籤:Python json 熊猫 文件 csvtojson
