[Pandas] DataFrame을 JSON으로 저장하고 JSON을 DataFrame으로로드
구체적인 흐름으로서는 Python상에서 데이터 작성(주기, state, system name 등) → pandas.DataFrame을 작성 → DataFrame을 JSON으로 저장하고 나중에 JSON을 DataFrame으로 읽는다.
DataFrame을 JSON으로 내보내기
우선 DataFrame을 준비

이것을 JSON으로 저장하려면
path = "/data_storage/my_data.json"
dataframe.to_json(path, orient="index", indent="4", double_precision=15)
먼저
path
에 저장처와 파일명, 확장자(물론 .json)를 입력. 옵션에 대해서는 orient="index"
로 Series 마다 보존하고 있어 indend="4"
로 Series 마다 JSON 파일내에서 개행해 준다. 결과는 이런 느낌{
"0":{
"mu":0.012150585609624,
"system":"Earth_Moon",
"family":"L2_Butterfly_Northern",
"Period":11.555291205753774,
"Jacobi":2.745412360915097,
"Stability":1.000000000003475,
"state_x":0.940999792653558,
"state_y":-1.02198464448071e-21,
"state_z":0.509474299789634,
"state_vx":0.000000000000002,
"state_vy":-0.12496802037539,
"state_vz":0.000000000000028
},
"1":{
"mu":0.012150585609624,
"system":"Earth_Moon",
"family":"L2_Butterfly_Northern",
"Period":11.545291205753774,
"Jacobi":2.747618763012661,
"Stability":1.000000000003555,
"state_x":0.942459953010378,
"state_y":-5.01931178153858e-20,
"state_z":0.507105298571933,
"state_vx":0.000000000000002,
"state_vy":-0.126877713881638,
"state_vz":0.000000000000035
},
JSON을 DataFrame으로로드
작성한 JSON을 읽을 때의 주의점으로서는,
pd.read_json()
의 옵션에 저장했을 때와 같은 orient
의 옵션을 사용하는 것.import pandas as pd
path = "/data_storage/my_data.json"
df_loaded = pd.read_json(path, orient="index")
이
orient
가 올바르지 않으면 columns와 rows가 바뀌어 읽어 버린다 :
Reference
이 문제에 관하여([Pandas] DataFrame을 JSON으로 저장하고 JSON을 DataFrame으로로드), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Yuricst/items/1abd9d643ec10ada1e38텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)