Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame([('bird', 2, 2),
                   ('mammal', 4, np.nan),
                   ('arthropod', 8, 0),
                   ('bird', 2, np.nan)],
                  index=('eagle', 'tiger', 'spider', 'duck'),
                  columns=('species', 'legs', 'wings'))
df
Out[2]:
species legs wings
eagle bird 2 2.0
tiger mammal 4 NaN
spider arthropod 8 0.0
duck bird 2 NaN

Pandas: Dataframe - mode.

By default, missing values are not considered, and the mode of wings are both 0 and 2.
The second row of species and legs contains NaN, because they have only one mode, but
the DataFrame has two rows.

In [3]:
df.mode()
Out[3]:
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).

In [4]:
df.mode(dropna=False)
Out[4]:
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.

In [5]:
df.mode(numeric_only=True)
Out[5]:
legs wings
0 2.0 0.0
1 NaN 2.0

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

In [6]:
df.mode(axis='columns', numeric_only=True)
Out[6]:
0 1
eagle 2.0 NaN
tiger 4.0 NaN
spider 0.0 8.0
duck 2.0 NaN