728x90
반응형
In [12]:
import pandas as pd
import numpy as np
In [3]:
url = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/04_Apply/Students_Alcohol_Consumption/student-mat.csv'
df = pd.read_csv(url,sep=',')
df.head()
Out[3]:
In [7]:
# loc을 사용해서 col의 name으로 slicing
df2 = df.loc[:,'school':'guardian']
df2[:5]
Out[7]:
In [14]:
Cap = lambda x:x.capitalize()
In [16]:
df2["Mjob"].apply(Cap)
df2['Fjob'].apply(Cap)
Out[16]:
In [22]:
df2.tail()
Out[22]:
In [24]:
df2["Mjob"] = df2["Mjob"].apply(Cap)
df2['Fjob'] = df2['Fjob'].apply(Cap)
df2.tail()
Out[24]:
In [25]:
def majority(x):
if x > 17:
return True
else:
return False
In [27]:
df2['legal_drinker'] = df2['age'].apply(majority)
df2[:5]
Out[27]:
In [33]:
def multiple10(x):
if type(x) == int:
return x * 10
return x
In [36]:
# applymap : Apply a function to a Dataframe elementwise.
# 각각 연산해주기 위해서 applymap 이용
df2.applymap(multiple10).head()
Out[36]:
728x90
반응형