5

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
Chris Macaluso
  • 1,372
  • 2
  • 14
  • 33

3 Answers3

9

Do it in two steps:

dummies = pd.get_dummies(df['Mfr Number'])
dummies.values[dummies != 0] = df['Quantity']
gmds
  • 19,325
  • 4
  • 32
  • 58
  • 1
    I get the output as int. But it wouldnt convert to float. Have you faced a simlar situation too? – Sagar Dawda Jun 06 '19 at 15:05
  • You are trying to get a float? `dataframe_obj.rename(lambda col: col.replace('.0', ''), axis='columns')` Is that what you need? – KLDavenport Sep 25 '20 at 02:01
4

Check with str.get_dummies and mul

df.Number.str.get_dummies().mul(df.quantity,0)
   40.50699.0095  40.50699.0100    ...      MWS0460MB  TM-120-6X
0              0              0    ...              1          0
1              0              0    ...              0          3
2              5              0    ...              0          0
3              0              1    ...              0          0
4              0              0    ...              0          0
[5 rows x 5 columns]
BENY
  • 317,841
  • 20
  • 164
  • 234
0
df = pd.get_dummies(df, columns = ['Mfr Number'])
for col in df.columns[2:]:
    df[col] = df[col]*df['quantity']