본문 바로가기
Python_Wiki/Python_Library

ast : 파이썬 자료형이 문자열 안에 있는 경우, 자료형 꺼내기

by yj-data 2025. 7. 15.

문자열로 되었는데, 안에 파이썬자료형이 들어가 있는경우, 그것을 파이썬 자료형으로 바꿔주는 라이브러리

import ast
print(ast.literal_eval(df[col_name][index]))

 

#example
data['crew'][0] #'[{"credit_id": "52fe48009251416c750aca23", "department": "Editing", "gender": 0, "id": 1721, "job": "Editor", "name": "Stephen E. Rivkin"}, {"credit_id": "539c47ecc3a36810e3001f87", "department": "Art", "gender": 2, "id": 496, "job": "Production Design", "name": "Rick Carter"}, {"credit_id": "54491c89c3a3680fb4001cf7", "department": "Sound", "gender": 0, "id": 900, "job": "Sound Designer", ...'

data['crew'] = data['crew'].apply(ast.literal_eval)  #apply사용
#이렇게 하면 문자열이었던 각 값이 파이썬 자료형 값으로 대체됨

'Python_Wiki > Python_Library' 카테고리의 다른 글

pandas: category 자료형으로 바꾸기  (0) 2025.07.16
데이터 마스킹 mask  (0) 2025.07.16
pandas: 시각화  (0) 2025.07.15
pandas: read_excel  (0) 2025.07.15
Plotly 기초 / 스타일 설정하기  (1) 2025.07.14