728x90
def extract_year(row):
return row.split('-')[0]
df['year'] = df['yyyy-mm-dd'].apply(extract_year)
06. 에서 했던거 이게 apply함수의 기본이래
def get_age(year, current_year):
return current_year - int(year)
df['age'] = df['year'].apply(get_age, current_year = 2020)
생년으로 나이 구해서 넣기.
def get_introduce(age, prefix, suffix):
return prefix + str(age) + suffix
df['introduce'] = df.apply(get_introduce, prefix = "I am", suffix = "years old")
자기소개 추가
def get_introduce_2(row):
return "I was born in " + str(row.year) + "my age is " + str(row.age)
df.introduce = df.apply(get_introduce_2, axis =1)
row를 신기하게 받는다. 오
728x90
'AI > Pandas 기초' 카테고리의 다른 글
12. 컬럼 내 유니크한 값 뽑아내고 갯수 확인하기 (0) | 2020.04.24 |
---|---|
11. map, applymap 함수 활용 (0) | 2020.04.24 |
09. NaN 찾아서 다른 값으로 변경하기 (0) | 2020.04.24 |
08. 중복 데이터 삭제하기 (0) | 2020.04.24 |
07. 데이터 그룹 만들기 (0) | 2020.04.24 |