python 分类变量转为哑变量_python 虚拟变量
# -*- coding: utf-8 -*-
import pandas
data = pandas.read_csv(
'D:\\PDA\\4.18\\data.csv',
encoding='utf8'
)
data['Education Level'].drop_duplicates()
"""
博士后? ? 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
}
data['Education Level Map'] = data[
'Education Level'
].map(
educationLevelDict
)
data['Gender'].drop_duplicates()
dummies = pandas.get_dummies(
data,
columns=['Gender'],
prefix=['Gender'],
prefix_sep="_",
dummy_na=False,
drop_first=False
)
dummies['Gender'] = data['Gender']
總結
以上是生活随笔為你收集整理的python 分类变量转为哑变量_python 虚拟变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: alert点击完确定关闭浏览器窗口_关务
- 下一篇: vue后端必须接口吗_前后端分离模式,后