Skip to content

Commit fc46fb7

Browse files
marcosrullanjreback
authored andcommitted
DOC: Added examples to the IndexOpsmixin.value_counts() docstring (pandas-dev#20145)
1 parent b12b7ae commit fc46fb7

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

pandas/core/base.py

+55-4
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ def map_f(values, f):
10081008
def value_counts(self, normalize=False, sort=True, ascending=False,
10091009
bins=None, dropna=True):
10101010
"""
1011-
Returns object containing counts of unique values.
1011+
Return a Series containing counts of unique values.
10121012
10131013
The resulting object will be in descending order so that the
10141014
first element is the most frequently-occurring element.
@@ -1020,18 +1020,69 @@ def value_counts(self, normalize=False, sort=True, ascending=False,
10201020
If True then the object returned will contain the relative
10211021
frequencies of the unique values.
10221022
sort : boolean, default True
1023-
Sort by values
1023+
Sort by values.
10241024
ascending : boolean, default False
1025-
Sort in ascending order
1025+
Sort in ascending order.
10261026
bins : integer, optional
10271027
Rather than count values, group them into half-open bins,
1028-
a convenience for pd.cut, only works with numeric data
1028+
a convenience for ``pd.cut``, only works with numeric data.
10291029
dropna : boolean, default True
10301030
Don't include counts of NaN.
10311031
10321032
Returns
10331033
-------
10341034
counts : Series
1035+
1036+
See Also
1037+
--------
1038+
Series.count: number of non-NA elements in a Series
1039+
DataFrame.count: number of non-NA elements in a DataFrame
1040+
1041+
Examples
1042+
--------
1043+
>>> index = pd.Index([3, 1, 2, 3, 4, np.nan])
1044+
>>> index.value_counts()
1045+
3.0 2
1046+
4.0 1
1047+
2.0 1
1048+
1.0 1
1049+
dtype: int64
1050+
1051+
With `normalize` set to `True`, returns the relative frequency by
1052+
dividing all values by the sum of values.
1053+
1054+
>>> s = pd.Series([3, 1, 2, 3, 4, np.nan])
1055+
>>> s.value_counts(normalize=True)
1056+
3.0 0.4
1057+
4.0 0.2
1058+
2.0 0.2
1059+
1.0 0.2
1060+
dtype: float64
1061+
1062+
**bins**
1063+
1064+
Bins can be useful for going from a continuous variable to a
1065+
categorical variable; instead of counting unique
1066+
apparitions of values, divide the index in the specified
1067+
number of half-open bins.
1068+
1069+
>>> s.value_counts(bins=3)
1070+
(2.0, 3.0] 2
1071+
(0.996, 2.0] 2
1072+
(3.0, 4.0] 1
1073+
dtype: int64
1074+
1075+
**dropna**
1076+
1077+
With `dropna` set to `False` we can also see NaN index values.
1078+
1079+
>>> s.value_counts(dropna=False)
1080+
3.0 2
1081+
NaN 1
1082+
4.0 1
1083+
2.0 1
1084+
1.0 1
1085+
dtype: int64
10351086
"""
10361087
from pandas.core.algorithms import value_counts
10371088
result = value_counts(self, sort=sort, ascending=ascending,

0 commit comments

Comments
 (0)