### 数值型数据

#### 自定义函数 + 循环遍历

```df1 = df.copy()
def myfun(x):
if x>90:
return 'A'
elif x>=80 and x<90:
return 'B'
elif x>=70 and x<80:
return 'C'
elif x>=60 and x<70:
return 'D'
else:
return 'E'

df1['Score_Label'] = None
for i in range(len(df1)):
df1.iloc[i,3] = myfun(df1.iloc[i,2])```

#### 自定义函数 + map

```df2 = df.copy()
def mapfun(x):
if x>90:
return 'A'
elif x>=80 and x<90:
return 'B'
elif x>=70 and x<80:
return 'C'
elif x>=60 and x<70:
return 'D'
else:
return 'E'
df2['Score_Label'] = df2['Score'].map(mapfun)```

#### 自定义函数 + apply

```df3 = df.copy()
df3['Score_Label'] = df3['Score'].apply(lambda x: 'A' if x > 90 else (
'B' if 90 > x >= 80 else ('C' if 80 > x >= 70 else ('D' if 70 > x >= 60 else 'E'))))```

#### 使用 pd.cut

```df4 = df.copy()
bins = [0, 59, 70, 80, 100]
df4['Score_Label'] = pd.cut(df4['Score'], bins)```

```df4['Score_Label_new'] = pd.cut(df4['Score'], bins, labels=[
'low', 'middle', 'good', 'perfect'])```

#### 使用 sklearn 二值化

```df5 = df.copy()
binerize = Binarizer(threshold = 60)
trans = binerize.fit_transform(np.array(df1['Score']).reshape(-1,1))
df5['Score_Label'] = trans```

### 文本型数据

#### 使用 replace

```df6 = df.copy()
df6['Sex_Label'] = df6['Sex'].replace(['Male','Female'],[0,1])```

```df6 = df.copy()
value = df6['Course Name'].value_counts()
value_map = dict((v, i) for i,v in enumerate(value.index))
df6['Course Name_Label'] = df6.replace({'Course Name':value_map})['Course Name']```

#### 使用map

```df7 = df.copy()
Map = {elem:index for index,elem in enumerate(set(df["Course Name"]))}
df7['Course Name_Label'] = df7['Course Name'].map(Map)```

#### 使用astype

```df8 = df.copy()
value = df8['Course Name'].astype('category')
df8['Course Name_Label'] = value.cat.codes```

#### 使用 sklearn

```from sklearn.preprocessing import LabelEncoder
df9 = df.copy()
le = LabelEncoder()
le.fit(df9['Sex'])
df9['Sex_Label'] = le.transform(df9['Sex'])
le.fit(df9['Course Name'])
df9['Course Name_Label'] = le.transform(df9['Course Name'])```

```df9 = df.copy()
le = OrdinalEncoder()
le.fit(df9[['Sex','Course Name']])
df9[['Sex_Label','Course Name_Label']] = le.transform(df9[['Sex','Course Name']])```

#### 使用factorize

```df10 = df.copy()
df10['Course Name_Label'] = pd.factorize(df10['Course Name'])[0]```

```df10 = df.copy()
cat_columns = df10.select_dtypes(['object']).columns
df10[['Sex_Label', 'Course Name_Label']] = df10[cat_columns].apply(
lambda x: pd.factorize(x)[0])```