Hi there!

I am a student studying computer science.

AI/Pandas 기초

10. apply 함수 활용

만능성구 2020. 4. 24. 19:27
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