Task : birthday 컬럼으로 나이 및 연령대 도출하기
실행 및 진행 사항 정리
# birthday 데이터 타입 변환 (datetime)
final_df_copy['birthday'] = pd.to_datetime(final_df_copy['birthday'], errors='coerce')
# 나이 컬럼 출력
from datetime import datetime
today = datetime.today()
def age(birthday):
if pd.notnull(birthday):
return today.year - birthday.year
else:
return None
final_df_copy['age'] = final_df_copy['birthday'].apply(age)
Python
복사
# 연령대 컬럼 출력
def categorize_age(age):
if age is None:
return '알 수 없음'
elif age < 20:
return '10대 이하'
elif age < 30:
return '20대'
elif age < 40:
return '30대'
elif age < 50:
return '40대'
elif age < 60:
return '50대'
else:
return '60대'
final_df_copy['age_group'] = final_df_copy['age'].apply(categorize_age)
Python
복사
if 조건문을 활용한 연령대 컬럼 추가 생성
결과
나이 및 연령대 컬럼 생성
