Calculate the Mode in Python

Your goal

You need to calculate the mode of discrete or categorical data in Python.

Step-by-step tutorial

There are different ways to do this, depending on the data representation you're working with.

Approach 1: List data

For Python lists, you can use either the mode or multimode function from the statistics module in the Python Standard Library. The first always returns a single mode if there's exactly one. Its behavior otherwise depends on your version of Python; see the link to the documentation in the Related section below. The second function, multimode returns all modes, but is only available starting with Python 3.8.

Here's an example using mode:

>>> import statistics
>>> data = ['apple', 'banana', 'cherry', 'banana', 'guava', 'cherry', 'cherry']
>>> statistics.mode(data)
'cherry'

Here's an example using multimode:

>>> import statistics
>>> data = ['apple', 'cherry', 'cherry', 'apple', 'grape']
>>> statistics.multimode(data)
['apple', 'cherry']

Approach 2: Pandas DataFrame

For a Pandas DataFrame, we typically want to grab a single column (which is a Pandas Series) and run mode on that:

>>> import pandas as pd
>>> crime_data = pd.read_csv("crime-sf-2016.csv")
>>> crime_data
        IncidntNum        Category                                           Descript  ...          Y                               Location            PdId
0        120058272     WEAPON LAWS                          POSS OF PROHIBITED WEAPON  ...  37.775421   (37.775420706711, -122.403404791479)  12005827212120
1        120058272     WEAPON LAWS     FIREARM, LOADED, IN VEHICLE, POSSESSION OR USE  ...  37.775421   (37.775420706711, -122.403404791479)  12005827212168
2        141059263        WARRANTS                                     WARRANT ARREST  ...  37.729981  (37.7299809672996, -122.388856204292)  14105926363010
3        160013662    NON-CRIMINAL                                      LOST PROPERTY  ...  37.785788  (37.7857883766888, -122.412970537591)  16001366271000
4        160002740    NON-CRIMINAL                                      LOST PROPERTY  ...  37.765050  (37.7650501214668, -122.419671780296)  16000274071000
...            ...             ...                                                ...  ...        ...                                    ...             ...
150495   161061000         ASSAULT                                            BATTERY  ...  37.771428  (37.7714278595913, -122.453981622365)  16106100004134
150496   176000742    NON-CRIMINAL                                      LOST PROPERTY  ...  37.796626  (37.7966261239618, -122.401857374739)  17600074271000
150497   176000758   LARCENY/THEFT                            PETTY THEFT OF PROPERTY  ...  37.790673   (37.7906727649886, -122.41226909106)  17600075806372
150498   176000764   LARCENY/THEFT                            GRAND THEFT OF PROPERTY  ...  37.788275  (37.7882745285785, -122.406658711008)  17600076406374
150499   179002868  OTHER OFFENSES  FRAUDULENT GAME OR TRICK, OBTAINING MONEY OR P...  ...  37.775421   (37.775420706711, -122.403404791479)  17900286809024

[150500 rows x 13 columns]
>>> crime_data["Category"].mode()
0    LARCENY/THEFT
dtype: object