Skip to content

DOC: add demo of factorize #5731

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
Dec 18, 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
22 changes: 22 additions & 0 deletions doc/source/reshaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,25 @@ This function is often used along with discretization functions like ``cut``:


get_dummies(cut(values, bins))

Factorizing values
------------------

To encode 1-d values as an enumerated type use ``factorize``:

.. ipython:: python

x = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
x
labels, uniques = pd.factorize(x)
labels
uniques

Note that ``factorize`` is similar to ``numpy.unique``, but differs in its
handling of NaN:

.. ipython:: python

pd.factorize(x, sort=True)
np.unique(x, return_inverse=True)[::-1]