简体   繁体   English

dataframe 中的 2 个虚拟变量

[英]2 dummy variables in a dataframe

I have a dataframe x_0 of 5 numeric variables and 2 categorical variables: ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7']我有 5 个数值变量和 2 个分类变量的 dataframe x_0:['v1','v2','v3','v4','v5','v6','v7']

I want to convert v6 and v7 to dummy variables.我想将 v6 和 v7 转换为虚拟变量。 I have tried with the following code but I receive a traceback:我已尝试使用以下代码,但收到回溯:

x = pd.concat([x_0['v1', 'v2', 'v3', 'v4', 'v5'], pd.get_dummies(x_0['v6', 'v7'])], axis = 1)

Use double [] for selecting by multiple columns names:使用 double []选择多个列名:

x = pd.concat([x_0[['v1', 'v2', 'v3', 'v4', 'v5']], 
               pd.get_dummies(x_0[['v6', 'v7']])], axis = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据虚拟变量拆分数据框 - How to split a dataframe based on dummy variables 如何将多个虚拟变量连接到 dataframe? - How to concat multiple dummy variables to dataframe? 为 pandas dataframe 中的序数创建虚拟变量 - Creating dummy variables for ordinals in pandas dataframe 如何从 pandas dataframe 反转虚拟变量 - How to reverse a dummy variables from a pandas dataframe 如何将二进制变量的DataFrame列转换为多个虚拟变量列 - How to turn a DataFrame column of binary variables into multiple columns of dummy variables 有没有一种方法可以从列表字典中创建虚拟变量的数据框? - Is there a method for creating dataframe of dummy variables from a dictionary of lists? 循环遍历Pandas Dataframe以创建虚拟变量(1或0输入)的有效方法 - Efficient way to loop over Pandas Dataframe to make dummy variables (1 or 0 input) 如何将 Pandas 数据帧中 Categorica 类型的所有列编码为虚拟变量 - How to Encode All Columns of type Categorica in a Pandas Dataframe as Dummy Variables 使用数值的虚拟变量创建固定大小的数据框 - Create dataframe of fixed size with dummy variables for numerical values 为什么随机森林要花更长的时间才能使数据框具有虚拟变量? - Why does a Random Forest take longer to fit a dataframe with dummy variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM