Skip to content

Commit 2d86b05

Browse files
committed
DOC: add demo of factorize
1 parent f6fd509 commit 2d86b05

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/source/reshaping.rst

+22
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,25 @@ This function is often used along with discretization functions like ``cut``:
417417
418418
419419
get_dummies(cut(values, bins))
420+
421+
Factorizing values
422+
------------------
423+
424+
To encode 1-d values as an enumerated type use ``factorize``:
425+
426+
.. ipython:: python
427+
428+
x = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
429+
x
430+
labels, uniques = pd.factorize(x)
431+
labels
432+
uniques
433+
434+
Note that ``factorize`` is similar to ``numpy.unique``, but differs in its
435+
handling of NaN:
436+
437+
.. ipython:: python
438+
439+
pd.factorize(x, sort=True)
440+
np.unique(x, return_inverse=True)[::-1]
441+

0 commit comments

Comments
 (0)