python 虚拟变量

# -*- coding: utf-8 -*-

import pandas

data = pandas.read_csv(

'D:\\PDA\\4.18\\data.csv',

encoding='utf8'

)

python 虚拟变量_第1张图片

data['Education Level'].drop_duplicates()

python 虚拟变量_第2张图片

"""

博士后    Post-Doc

博士      Doctorate

硕士      Master's Degree

学士      Bachelor's Degree

副学士    Associate's Degree

专业院校  Some College

职业学校  Trade School

高中      High School

小学      Grade School

"""

educationLevelDict = {

'Post-Doc': 9,

'Doctorate': 8,

'Master\'s Degree': 7,

'Bachelor\'s Degree': 6,

'Associate\'s Degree': 5,

'Some College': 4,

'Trade School': 3,

'High School': 2,

'Grade School': 1

}

python 虚拟变量_第3张图片

data['Education Level Map'] = data[

'Education Level'

].map(

educationLevelDict

)

python 虚拟变量_第4张图片

data['Gender'].drop_duplicates()

python 虚拟变量_第5张图片
python 虚拟变量_第6张图片

dummies = pandas.get_dummies(

data,

columns=['Gender'],

prefix=['Gender'],

prefix_sep="_",

dummy_na=False,

drop_first=False

)

python 虚拟变量_第7张图片

dummies['Gender'] = data['Gender']

python 虚拟变量_第8张图片

你可能感兴趣的:(python 虚拟变量)