Skip to content

Commit 65b1d91

Browse files
committed
Fixup 10min
1 parent 009097f commit 65b1d91

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

doc/source/10min.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,27 +640,44 @@ Categoricals
640640
------------
641641

642642
Since version 0.15, pandas can include categorical data in a ``DataFrame``. For full docs, see the
643-
:ref:`categorical introduction <categorical>` and the :ref:`API documentation <api.categorical>` .
643+
:ref:`categorical introduction <categorical>` and the :ref:`API documentation <api.categorical>`.
644644

645645
.. ipython:: python
646646
647647
df = pd.DataFrame({"id":[1,2,3,4,5,6], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
648648
649-
# convert the raw grades to a categorical
649+
Convert the raw grades to a categorical data type.
650+
651+
.. ipython:: python
652+
650653
df["grade"] = df["raw_grade"].astype("category")
651654
df["grade"]
652655
653-
# Rename the categories inplace
656+
Rename the categories to more meaningful names (assigning to ``Series.cat.categories`` is inplace!)
657+
658+
.. ipython:: python
659+
654660
df["grade"].cat.categories = ["very good", "good", "very bad"]
655661
656-
# Reorder the categories and simultaneously add the missing categories
662+
Reorder the categories and simultaneously add the missing categories (methods under ``Series
663+
.cat`` return a new ``Series`` per default).
664+
665+
.. ipython:: python
666+
657667
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium", "good", "very good"])
658668
df["grade"]
659-
# Sorting is per order in the categories
669+
670+
Sorting is per order in the categories, not lexical order.
671+
672+
.. ipython:: python
673+
660674
df.sort("grade")
661-
# groupby shows also empty categories
662-
df.groupby("grade").size()
663675
676+
Grouping by a categorical column shows also empty categories.
677+
678+
.. ipython:: python
679+
680+
df.groupby("grade").size()
664681
665682
666683
Plotting

0 commit comments

Comments
 (0)