Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

python - Pandas: Get Dummies

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

question from:https://stackoverflow.com/questions/36285155/pandas-get-dummies

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...