파이썬에서 JSON Pandas dataframe으로 변환하기

KeyEun Lee·2023년 12월 29일

20231229 TIL

요구사항

  • GPT json object output을 pandas dataframe으로 변환해서 저장해주세요 (그냥 pd.DataFrame(data)하면, ugly하게 저장됨)

Solution: pd.json_normalize()

import pandas as pd

# This is the object structure you've provided
data = [
    {'level_0': {'point': 'A'},
     'level_1': {'president': None},
     'level_2': {'press': {'name': False,
                           'year': None,
                           'type': None},
                 'journalist': {'name': False, 
                               'year': None, 
                               'type': None}
                 },
     'link': 'https://www.abc.com'}
    # ... you can add more dictionary items here
]

# Flatten the nested structure and create a DataFrame
df = pd.json_normalize(data)
  • depth를 기가막히게 다 알아서 column으로 찢어준다!
profile
느리지만 꾸준히 공부합니다 https://keyeun.com/

0개의 댓글