티스토리 뷰

반응형

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

 

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함