Pandas: Get Dummies

可紊 提交于 2019-11-26 09:53:47

问题


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


回答1:


You can try :

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


来源:https://stackoverflow.com/questions/36285155/pandas-get-dummies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!