Skip to content

Commit 19cc3a9

Browse files
committed
DOC add get_dummies to reshaping.rst
examples shamelessly from Wes' book
1 parent ac1609e commit 19cc3a9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

doc/source/reshaping.rst

+41
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,44 @@ Alternatively we can specify custom bin-edges:
361361
.. ipython:: python
362362
363363
cut(ages, bins=[0, 18, 35, 70])
364+
365+
366+
.. _reshaping.dummies:
367+
368+
Computing indicator / dummy variables
369+
-------------------------------------
370+
371+
To convert a categorical variable into a "dummy" or "indicator" DataFrame, for example
372+
a column in a DataFrame (a Series) which has ``k`` distinct values, can derive a DataFrame
373+
containing ``k`` columns of 1s and 0s:
374+
375+
.. ipython:: python
376+
377+
df = DataFrame({'key': list('bbacab'), 'data1': range(6)})
378+
379+
380+
get_dummies(df['key'])
381+
382+
Sometimes it's useful to prefix the column names, for example when merging the result
383+
with the original DataFrame:
384+
385+
.. ipython:: python
386+
387+
dummies = get_dummies(df['key'], prefix='key')
388+
dummies
389+
390+
391+
df[['data']].join(dummies)
392+
393+
This function is often used along with discretization functions like ``cut``:
394+
395+
.. ipython:: python
396+
397+
values = randn(10)
398+
values
399+
400+
401+
bins = [0, 0.2, 0.4, 0.6, 0.8, 1]
402+
403+
404+
get_dummies(cut(values, bins))

0 commit comments

Comments
 (0)