-1


Hi there!
I m working on python with pandas' get_dummies function and I try to transform an int into a vector like for example with a 5 categories feature :
1 -> [1,0,0,0,0]
2 -> [0,1,0,0,0]
...
Does a function exist for that? If not I can built a function but I just ask before reinventing the wheel.

Thanks !

adrien22
  • 21
  • 1
  • 4
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jan 16 '18 at 14:23

2 Answers2

3

Just cast the relevant Series to a string and then use get_dummies as usual.

pd.get_dummies(df['col'].astype(str))
0

I think it's so easy you should just write a simple function to do that, instead of asking. Here is one of countless ways to do this.

import numpy as np
    def get_dumm(lenn, num):
        arr = np.zeros(lenn, dtype='bool_') #replace type with 'int8' if needed
        arr[num - 1] = True #replace True with 1 if type of arr is 'int8'
        return arr
get_dumm(5,3)

Output:

array([False, False, True, False, False], dtype=bool)

Or if you use int8:

array([0, 0, 1, 0, 0], dtype=int8)
Sqoshu
  • 944
  • 2
  • 9
  • 16