变换虚拟变量
# 导入包
import pandas as pd
import numpy as np
# 在同一个cell里同时输出多条语句
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
# 建立数据集
df = pd.DataFrame([
['green', 'M', 10.1, 'class1'],
['red', 'L', 13.5, 'class2'],
['blue', 'XL', 15.3, 'class1']])
df.columns = ['color', 'size', 'prize', 'class label']
df
image.png
color=pd.get_dummies(df['color'])
df=pd.merge(df,color,left_index=True,right_index=True)
image.png
网友评论