简体   繁体   English

为 pandas dataframe 中的序数创建虚拟变量

[英]Creating dummy variables for ordinals in pandas dataframe

I am trying to create dummy variables in python in the pandas dataframe format.我正在尝试以 pandas dataframe 格式在 python 中创建虚拟变量。 I have a variable called "Weight Group" and I want to transform the variables like so:我有一个名为“Weight Group”的变量,我想像这样转换变量:

Before transformation:改造前:

    Weight_Group
0      1
1      5
2      4
3      2
4      2
5      3
6      1

After transformation:改造后:

    WD_1  WD_2  WD_3  WD_4  WD_5
0      1    0     0     0     0
1      1    1     1     1     1
2      1    1     1     1     0 
3      1    1     0     0     0
4      1    1     0     0     0
5      1    1     1     0     0
6      1    0     0     0     0

I know that pandas has the get_dummies() function that creates dummy variables, but it doesn't give me the functionality that I want, where someone in weight group 3 has ones in the WG_1, WG_2, and WG_3 columns.我知道 pandas 有 get_dummies() function 可以创建虚拟变量,但它没有给我想要的功能,其中权重组 3 中的某个人在 WG_1、WG_2 和 WG_3 列中有功能。 I have a lot of data points so a fast method would be great.我有很多数据点,所以快速的方法会很棒。 If anyone has any ideas on how I can implement this I would really appreciate it!如果有人对我如何实现这一点有任何想法,我将不胜感激!

You can call pd.get_dummies() and then replace your 0 tallies with NaN and use bfill() (plus a bit of extra cleanup for display):你可以调用pd.get_dummies()然后用NaN替换你的0计数并使用bfill() (加上一些额外的显示清理):

pd.get_dummies(df['Weight_Group'], prefix='WD').replace(0,np.nan).bfill(axis=1).fillna(0).astype(int)

Yields:产量:

   WD_1  WD_2  WD_3  WD_4  WD_5
0     1     0     0     0     0
1     1     1     1     1     1
2     1     1     1     1     0
3     1     1     0     0     0
4     1     1     0     0     0
5     1     1     1     0     0
6     1     0     0     0     0

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

相关问题 在 pandas 中为 python 创建虚拟变量 - Creating dummy variables in pandas for python 从 pandas 中的字符串列创建虚拟变量 - Creating dummy variables from a string column in pandas 如何从 pandas dataframe 反转虚拟变量 - How to reverse a dummy variables from a pandas dataframe python 如何将序数添加到 pandas dataframe - python How to add ordinals number to pandas dataframe 使用 Pandas 在 Python 中为具有连续和分类变量的数据集创建虚拟变量 - Creating dummy variables for dataset with continuous and categorical variables in Python using Pandas 有没有一种方法可以从列表字典中创建虚拟变量的数据框? - 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 dataframe 中的 2 个虚拟变量 - 2 dummy variables in a dataframe 使用pandas创建虚拟变量时,Jupyter笔记本内核会死掉 - Jupyter notebook kernel dies when creating dummy variables with pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM