json_normalize를 활용하여 json 형태의 데이터를 갖고 있는 열을 데이터 프레임으로 만든 후 reindex로 데이터 프레임의 열 설정을 해주는 과정입니다.
from pandas import json_normalize
import ast
# json_normalize로 Event_Value열 분리
def app_process:
json_transformed_df = json_normalize(required_union_df['Event_Value'].apply(to_dict).tolist()).reindex(columns=["order", "category", "cost"])
exploded_df = json_transformed_df.explode("category")
def to_dict(self, df):
string_df = str(df).replace("₩n", '').replace("₩", '').replace("\"[{", "[{").replace("}]\"", "}]")
return ast.literal_eval(string_df)
json 형태의 Event_Value열 안에 category의 경우 value로 다시 json list를 갖고 있었습니다. category의 값을 분리하기 위해 json list만큼 row를 만들어주는(category 이외의 값은 동일하게 복사, index는 category의 요소 수 만큼 동일한 값을 가짐) explode를 활용하였습니다.
다음번엔, 아래 참고에서 볼 수 있는 json_normalize의 record_path와 meta 변수를 활용하여 explode를 대체할 수 있을 듯 합니다.