[英]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.