Skip to content

DOC add get_dummies to reshaping.rst #5293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions doc/source/reshaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,44 @@ Alternatively we can specify custom bin-edges:
.. ipython:: python

cut(ages, bins=[0, 18, 35, 70])


.. _reshaping.dummies:

Computing indicator / dummy variables
-------------------------------------

To convert a categorical variable into a "dummy" or "indicator" DataFrame, for example
a column in a DataFrame (a Series) which has ``k`` distinct values, can derive a DataFrame
containing ``k`` columns of 1s and 0s:

.. ipython:: python

df = DataFrame({'key': list('bbacab'), 'data1': range(6)})


get_dummies(df['key'])

Sometimes it's useful to prefix the column names, for example when merging the result
with the original DataFrame:

.. ipython:: python

dummies = get_dummies(df['key'], prefix='key')
dummies


df[['data']].join(dummies)

This function is often used along with discretization functions like ``cut``:

.. ipython:: python

values = randn(10)
values


bins = [0, 0.2, 0.4, 0.6, 0.8, 1]


get_dummies(cut(values, bins))