import pandas as pd
df = pd.DataFrame([
['green', 'M', 10.1, '女','class1'],
['red', 'L', 13.5, '男','class2'],
['blue', 'XL', 15.3, '女','class1']])
df.columns = ['color', 'size', 'prize', 'sex','class label']
print(df)
size_mapping = {
'XL': 3,
'L': 2,
'M': 1}
df['size'] = df['size'].map(size_mapping)
class_mapping = {label:idx for idx,label in enumerate(set(df['class label']))}
df['class label'] = df['class label'].map(class_mapping)
sex_mapping={label:idx for idx,label in enumerate(set(df['sex']))}
df['sex']=df['sex'].map(sex_mapping)
print(df)
说明:对于有大小意义的离散特征,直接使用映射就可以了,{‘XL’:3,’L’:2,’M’:1 }