pandas -带有另一列中的值的get_dummies [英] Pandas - get_dummies with value from another column

查看:104
本文介绍了 pandas -带有另一列中的值的get_dummies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框.列Mfr Number是分类数据类型.我想在其上执行get_dummies或一种热编码,但是我希望它填充quantity列中的值,而不是在新列中填充1.所有其他新的虚拟对象"应在该行上保持为0.这可能吗?

I have a dataframe like below. The column Mfr Number is a categorical data type. I'd like to preform get_dummies or one hot encoding on it, but instead of filling in the new column with a 1 if it's from that row, I want it to fill in the value from the quantity column. All the other new 'dummies' should remain a 0 on that row. Is this possible?

    Datetime            Mfr Number                quantity
0   2016-03-15 07:02:00 MWS0460MB                 1
1   2016-03-15 07:03:00 TM-120-6X                 3
2   2016-03-15 08:33:00 40.50699.0095             5
3   2016-03-15 08:42:00 40.50699.0100             1
4   2016-03-15 08:46:00 CXS-04T098-00-0703R-1025  10

推荐答案

分两步进行:

dummies = pd.get_dummies(df['Mfr Number'])
dummies.values[dummies != 0] = df['Quantity']

这篇关于 pandas -带有另一列中的值的get_dummies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆