Pandas: Get Dummies

Michael Perdue picture Michael Perdue · Mar 29, 2016 · Viewed 73.3k times · Source

I have the following dataframe:

   amount  catcode    cid      cycle      date     di  feccandid    type
0   1000    E1600   N00029285   2014    2014-05-15  D   H8TX22107   24K
1   5000    G4600   N00026722   2014    2013-10-22  D   H4TX28046   24K
2      4    C2100   N00030676   2014    2014-03-26  D   H0MO07113   24Z

I want to make dummy variables for the values in column type. There about 15. I have tried this:

pd.get_dummies(df['type'])

And it returns this:

           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date                                    
2014-05-15  0    0    0    0    1    0    0    0    0
2013-10-22  0    0    0    0    1    0    0    0    0
2014-03-26  0    0    0    0    0    0    0    0    1

What I would like is to have a dummy variable column for each unique value in Type

Answer

Till picture Till · Mar 29, 2016

You can try :

df = pd.get_dummies(df, columns=['type'])