pandas - 将分类列转换为单个虚拟变量列
问题描述
考虑我有以下数据框:
Survived Pclass Sex Age Fare
0 0 3 male 22.0 7.2500
1 1 1 female 38.0 71.2833
2 1 3 female 26.0 7.9250
3 1 1 female 35.0 53.1000
4 0 3 male 35.0 8.0500
我使用 get_dummies() 函数来创建虚拟变量。代码和输出如下:
one_hot = pd.get_dummies(dataset, columns = ['Category'])
这将返回:
Survived Pclass Age Fare Sex_female Sex_male
0 0 3 22 7.2500 0 1
1 1 1 38 71.2833 1 0
2 1 3 26 7.9250 1 0
3 1 1 35 53.1000 1 0
4 0 3 35 8.0500 0 1
我想要的是 Sex 的单列,其值为 0 或 1 而不是 2 列。
有趣的是,当我在不同的数据帧上使用 get_dummies() 时,它就像我想要的那样工作。
对于以下数据框:
Category Message
0 ham Go until jurong point, crazy.. Available only ...
1 ham Ok lar... Joking wif u oni...
2 spam Free entry in 2 a wkly comp to win FA Cup final...
3 ham U dun say so early hor... U c already then say...
4 ham Nah I don't think he goes to usf, he lives aro...
使用代码:
one_hot = pd.get_dummies(dataset, columns = ['Category'])
它返回:
Message ... Category_spam
0 Go until jurong point, crazy.. Available only ... ... 0
1 Ok lar... Joking wif u oni... ... 0
2 Free entry in 2 a wkly comp to win FA Cup fina... ... 1
3 U dun say so early hor... U c already then say... ... 0
4 Nah I don't think he goes to usf, he lives aro... ... 0
- 为什么 get_dummies() 在这两个数据帧上的工作方式不同?
- 如何确保每次都能获得第二个输出?
解决方案
您可以通过以下多种方式进行操作:
from sklearn.preprocessing import LabelEncoder
lbl=LabelEncoder()
df['Sex_encoded'] = lbl.fit_transform(df['Sex'])
# using only pandas
df['Sex_encoded'] = df['Sex'].map({'male': 0, 'female': 1})
Survived Pclass Sex Age Fare Sex_encoded
0 0 3 male 22.0 7.2500 0
1 1 1 female 38.0 71.2833 1
2 1 3 female 26.0 7.9250 1
3 1 1 female 35.0 53.1000 1
4 0 3 male 35.0 8.0500 0
推荐阅读
- javascript - Google 电子表格中单元格文本的 sha3-256
- android - 如何在 Android Studio 中以编程方式将 apk 文件从 root/data/app/ 移动到 storage/emulated/0
- php - 用额外的步骤从字符串启动一个类?
- java - 无法在 Intellij 中导入 Maven 模块
- apache-spark - Spark 2 的性能偶数运行优于奇数
- java - 如何获取Java和C#中定义的元素之前和之后的所有元素
- php - Laravel VueJs Apache Virtualhost,显示Vue时虚拟主机名未出现在URL中
- javascript - 如何使页面的内容依赖于路由?
- elasticsearch - 无法使用分析器在复合查询中进行搜索
- prolog - 素数列表。为什么我的代码不起作用?