美文网首页
python虚拟变量变换get_dummies()

python虚拟变量变换get_dummies()

作者: 数据小白周红艳 | 来源:发表于2020-08-25 12:57 被阅读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

相关文章

网友评论

      本文标题:python虚拟变量变换get_dummies()

      本文链接:https://www.haomeiwen.com/subject/yduvjktx.html