Skip to content

Commit 12c8ec4

Browse files
authored
Add doc for counting categorical dtype (#59327)
* Add doc for counting categorical dtype * Move example to docstring instead * Remove backslash * add line * Undo the change in categorical.rst * Rename it to Categorical Dtypes * undo categorical.rst
1 parent 7e7735b commit 12c8ec4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pandas/core/base.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,34 @@ def value_counts(
10491049
4.0 1
10501050
NaN 1
10511051
Name: count, dtype: int64
1052+
1053+
**Categorical Dtypes**
1054+
1055+
Rows with categorical type will be counted as one group
1056+
if they have same categories and order.
1057+
In the example below, even though ``a``, ``c``, and ``d``
1058+
all have the same data types of ``category``,
1059+
only ``c`` and ``d`` will be counted as one group
1060+
since ``a`` doesn't have the same categories.
1061+
1062+
>>> df = pd.DataFrame({"a": [1], "b": ["2"], "c": [3], "d": [3]})
1063+
>>> df = df.astype({"a": "category", "c": "category", "d": "category"})
1064+
>>> df
1065+
a b c d
1066+
0 1 2 3 3
1067+
1068+
>>> df.dtypes
1069+
a category
1070+
b object
1071+
c category
1072+
d category
1073+
dtype: object
1074+
1075+
>>> df.dtypes.value_counts()
1076+
category 2
1077+
category 1
1078+
object 1
1079+
Name: count, dtype: int64
10521080
"""
10531081
return algorithms.value_counts_internal(
10541082
self,

0 commit comments

Comments
 (0)