python - Creating new binary columns from single string column in pandas -


i've seen before , can't remember function.

say have column "speed" , each row has 1 of these values:

'slow', 'normal', 'fast' 

how create new dataframe rows except column "speed" 3 columns: "slow" "normal" , "fast" has of rows labeled 1 in whichever column old "speed" column was. if had:

print df['speed'].ix[0] > 'normal' 

i not expect this:

print df['normal'].ix[0] >1  print df['slow'].ix[0] >0 

you can pd.get_dummies (docs):

in [37]: df = pd.dataframe(['slow', 'normal', 'fast', 'slow'], columns=['speed'])  in [38]: df out[38]:     speed 0    slow 1  normal 2    fast 3    slow  in [39]: pd.get_dummies(df['speed']) out[39]:    fast  normal  slow 0     0       0     1 1     0       1     0 2     1       0     0 3     0       0     1 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -