인공지능(Artificial Intelligence)/python
[pandas] JSON 파일 열기
HAN_PY
2021. 1. 13. 15:52
반응형
JSON 파일은 데이터 공유를 목적으로 개발된 특수한 파일 형식이다. 파이썬 딕셔너리와 비슷하게 'key:value' 구조를 갖는다. 예제는 다음과 같다. 기본적인 파이썬의 json 라이브러리를 사용하는 방식은 여기를 눌러서 확인하자. 우리는 판다스로 json을 데이터 프레임으로 바꿀 것이다.
다음의 json 내용이 있다고 가정해 보자.
# json_file.json 파일 내용
{
"name":{"pandas":"",
"NumPy":"",
"matplotlib":""},
"year":{"pandas":2008,
"NumPy":2006,
"matplotlib":2003},
"developer":{"pandas":"Wes Mckinneye",
"NumPy":"Travis Oliphant",
"matplotlib":"John D. Hunter"},
"opensource":{"pandas":"True",
"NumPy":"True",
"matplotlib":"True"}
}
코드는 아래와 같다
import pandas as pd
df = pd.read_json('./json_file.json')
print(df)
#output
name year developer opensource
NumPy 2006 Travis Oliphant True
matplotlib 2003 John D. Hunter True
pandas 2008 Wes Mckinneye True
공식문서는 아래와 같다. 더 많은 정보는 참고해 보자.
pandas.pydata.org/docs/user_guide/io.html#io-json-reader
IO tools (text, CSV, HDF5, …) — pandas 1.2.0 documentation
The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing av
pandas.pydata.org
반응형