Examples
import numpy as np
import pandas as pd
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
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.
df.mode()
Setting dropna=False NaN values are considered and they can be the mode (like for wings).
df.mode(dropna=False)
Setting numeric_only=True, only the mode of numeric columns is computed,
and columns of other types are ignored.
df.mode(numeric_only=True)
To compute the mode over columns and not rows, use the axis parameter:
df.mode(axis='columns', numeric_only=True)