pandas 1.4.2

ParametersReturnsBackRef
mode(self, axis: 'Axis' = 0, numeric_only: 'bool' = False, dropna: 'bool' = True) -> 'DataFrame'

The mode of a set of values is the value that appears most often. It can be multiple values.

Parameters

axis : {0 or 'index', 1 or 'columns'}, default 0

The axis to iterate over while searching for the mode:

numeric_only : bool, default False

If True, only apply to numeric columns.

dropna : bool, default True

Don't consider counts of NaN/NaT.

Returns

DataFrame

The modes of each column or row.

Get the mode(s) of each element along the selected axis.

See Also

Series.mode

Return the highest frequency value in a Series.

Series.value_counts

Return the counts of values in a Series.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame([('bird', 2, 2),
...  ('mammal', 4, np.nan),
...  ('arthropod', 8, 0),
...  ('bird', 2, np.nan)],
...  index=('falcon', 'horse', 'spider', 'ostrich'),
...  columns=('species', 'legs', 'wings'))
... df species legs wings falcon bird 2 2.0 horse mammal 4 NaN spider arthropod 8 0.0 ostrich bird 2 NaN

By default, missing values are not considered, and the mode of wings are both 0 and 2. Because the resulting DataFrame has two rows, the second row of species and legs contains NaN .

This example is valid syntax, but we were not able to check execution
>>> df.mode()
  species  legs  wings
0    bird   2.0    0.0
1     NaN   NaN    2.0

Setting dropna=False NaN values are considered and they can be the mode (like for wings).

This example is valid syntax, but we were not able to check execution
>>> df.mode(dropna=False)
  species  legs  wings
0    bird     2    NaN

Setting numeric_only=True , only the mode of numeric columns is computed, and columns of other types are ignored.

This example is valid syntax, but we were not able to check execution
>>> df.mode(numeric_only=True)
   legs  wings
0   2.0    0.0
1   NaN    2.0

To compute the mode over columns and not rows, use the axis parameter:

This example is valid syntax, but we were not able to check execution
>>> df.mode(axis='columns', numeric_only=True)
           0    1
falcon   2.0  NaN
horse    4.0  NaN
spider   0.0  8.0
ostrich  2.0  NaN
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.ops.flex_arith_method_FRAME.<locals>.f

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#10316
type: <class 'function'>
Commit: