python虚拟变量变换get_dummies()

2020-08-25  本文已影响0人  数据小白周红艳

变换虚拟变量

# 导入包
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
上一篇 下一篇

猜你喜欢

热点阅读