pyspark.pandas.Series.mode

Series.mode(dropna: bool = True) → pyspark.pandas.series.Series[source]

Return the mode(s) of the dataset.

Always returns Series even if only one value is returned.

Parameters
dropnabool, default True

Don’t consider counts of NaN/NaT.

Returns
Series

Modes of the Series.

Examples

>>> s = ps.Series([0, 0, 1, 1, 1, np.nan, np.nan, np.nan])
>>> s
0    0.0
1    0.0
2    1.0
3    1.0
4    1.0
5    NaN
6    NaN
7    NaN
dtype: float64
>>> s.mode()
0    1.0
dtype: float64

If there are several same modes, all items are shown

>>> s = ps.Series([0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,
...                np.nan, np.nan, np.nan])
>>> s
0     0.0
1     0.0
2     1.0
3     1.0
4     1.0
5     2.0
6     2.0
7     2.0
8     3.0
9     3.0
10    3.0
11    NaN
12    NaN
13    NaN
dtype: float64
>>> s.mode().sort_values()  

...  1.0
...  2.0
...  3.0
dtype: float64

With ‘dropna’ set to ‘False’, we can also see NaN in the result

>>> s.mode(False).sort_values()  

...  1.0
...  2.0
...  3.0
...  NaN
dtype: float64