Skip to content

Commit b246920

Browse files
committed
Merge pull request #9769 from alfonsomhc/patch-1
Added documentation for mode()
2 parents 10c933b + 761be0f commit b246920

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pandas/core/frame.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -4411,9 +4411,15 @@ def _get_agg_axis(self, axis_num):
44114411

44124412
def mode(self, axis=0, numeric_only=False):
44134413
"""
4414-
Gets the mode of each element along the axis selected. Empty if nothing
4414+
Gets the mode(s) of each element along the axis selected. Empty if nothing
44154415
has 2+ occurrences. Adds a row for each mode per label, fills in gaps
4416-
with nan.
4416+
with nan.
4417+
4418+
Note that there could be multiple values returned for the selected
4419+
axis (when more than one item share the maximum frequency), which is the
4420+
reason why a dataframe is returned. If you want to impute missing values
4421+
with the mode in a dataframe ``df``, you can just do this:
4422+
``df.fillna(df.mode().iloc[0])``
44174423
44184424
Parameters
44194425
----------
@@ -4426,6 +4432,14 @@ def mode(self, axis=0, numeric_only=False):
44264432
Returns
44274433
-------
44284434
modes : DataFrame (sorted)
4435+
4436+
Examples
4437+
--------
4438+
>>> df = pd.DataFrame({'A': [1, 2, 1, 2, 1, 2, 3]})
4439+
>>> df.mode()
4440+
A
4441+
0 1
4442+
1 2
44294443
"""
44304444
data = self if not numeric_only else self._get_numeric_data()
44314445
f = lambda s: s.mode()

0 commit comments

Comments
 (0)