Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - get_dummies with value from another column

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
like image 539
Chris Macaluso Avatar asked Dec 24 '22 00:12

Chris Macaluso


1 Answers

Do it in two steps:

dummies = pd.get_dummies(df['Mfr Number'])
dummies.values[dummies != 0] = df['Quantity']
like image 122
gmds Avatar answered Jan 04 '23 23:01

gmds