python_pandas_11 - 8BitsCoding/RobotMentor GitHub Wiki
data_list = [{'yyyy-mm-dd': '2000-06-27'},
{'yyyy-mm-dd': '2002-09-24'},
{'yyyy-mm-dd': '2005-12-20'}]
df = pd.DataFrame(data_list, columns = ['yyyy-mm-dd'])
def extract_year(column):
return column.split("-")[0]
df['year'] = df['yyyy-mm-dd'].apply(extract_year)
def get_age(year, current_year):
return current_year - int(year)
df['age'] = df['year'].apply(get_age, current_year=2018)
๋งค๊ฐ๋ณ์ ๋ฃ๊ธฐ
def get_introduce(age, prefix, suffix):
return prefix + str(age) + suffix
df['introduce'] = df['age'].apply(get_introduce, prefix="I am", suffix" years old")
์ฌ๋ฌ๊ฐ์ ๋งค๊ฐ๋ณ์ ๋ฃ๊ธฐ - 1
def get_introduce2(row):
return "I was born in " + str(row.year) + " my age is " + str(row.age)
df.introduce = df.apply(get_introduce2, axis = 1)
์ฌ๋ฌ๊ฐ์ ๋งค๊ฐ๋ณ์ ๋ฃ๊ธฐ - 2