Map : dictionary로 mapping 할때 주로 사용Apply : definition으로 함수를 사용자가 정의를 할때 주로 사용Unknown : N
Less than $40K : a
$40K - $60K : b
$60K - $80K : c
$80K - $120K : d
$120K +’ : e
dic = {
'Unknown' : 'N',
'Less than $40K' : 'a',
'$40K - $60K' : 'b',
'$60K - $80K' : 'c',
'$80K - $120K' : 'd',
'$120K +' : 'e'
}
df['newIncome'] = df.Income_Category.map(lambda x: dic[x])
Ans = df[['newIncome', 'Income_Category']]
Ans.head()
def changeCategory(x):
if x =='Unknown':
return 'N'
elif x == 'Less than $40K':
return 'a'
elif x == '$40K - $60K':
return 'b'
elif x == '$60K - $80K':
return 'c'
elif x == '$80K - $120K':
return 'd'
elif x == '$120K +' :
return 'e'
df['newIncome'] = df.Income_Category.apply(changeCategory)
Ans = df[['newIncome', 'Income_Category']]
Ans.head()
// : 나누기 한 후의 몫df['AgeState'] = df.Customer_Age.map(lambda x: x//10 *10)
Ans = df['AgeState'].value_counts().sort_index()
Ans
if else 문의 축약문 : 1 if 'Graduate' in x else 0x : 앞의 변수df['newEduLevel'] = df.Education_Level.map(lambda x : 1 if 'Graduate' in x else 0)
Ans = df['newEduLevel'].value_counts()
Ans
numpy numpy : 조건에 맞다면 true값, 그렇지 않다면 false값np.where(조건,true,False)
import numpy as np
df['newEduLevel'] = np.where( df.Education_Level.str.contains('Graduate'), 1, 0)
Ans = df['newEduLevel'].value_counts()
Ans
df['newLimit'] = df.Credit_Limit.map(lambda x : 1 if x>=4500 else 0)
Ans = df['newLimit'].value_counts()
Ans
df['newLimit'] = df.Credit_Limit.apply(lambda x : 1 if x>=4500 else 0)
Ans = df['newLimit'].value_counts()
Ans
def check(x):
if x.Marital_Status == 'Married' and x.Card_Category == 'Platinum':
return 1
else:
return 0
df['newState'] = df.apply(check,axis=1)
Ans = df['newState'].value_counts()
Ans
def changeGender(x)
if x == 'M':
return 'Male'
else:
return 'Female'
df['Gender'] = df['Gender'].apply(changeGenger)
Ans = df['Gender'].value_counts()
Ans