Skip to content

Informatics Practices · Ch 3 — Data Handling using Pandas – II

Calculating Mode

3.2.7

Calculating Mode

The mode is the value that appears most frequently in a dataset. While the mean and median give you a central tendency based on calculation or position, the mode tells you which value is most common — it is the only measure of central tendency that can be used with categorical data as well as numerical data.

In Pandas, the DataFrame.mode() function is used to find the mode. It returns the mode for each column (or row) of a DataFrame. If a column has multiple values that appear the same maximum number of times, the function will return all of them.

To get the mode of a specific column, you call .mode() on that Series. For example, to find the mode of the 'Hindi' marks column (whose values are shown in the table above):

>>> df['Hindi'].mode()
0    24
dtype: int64

The output shows that the mode of the Hindi marks is 24. Looking at the data, three students scored 24 marks in Hindi, which is more than any other score. For comparison, two students scored 25 marks, two students scored 22 marks, and one student each scored 23, 21, 20, 15, and 17 marks.

Note

The mode is not necessarily unique. If two or more values appear the same number of times (and that number is the highest frequency), all of them are modes. The .mode() function will return a Series containing all such values.

The textbook also includes two activities for practice: …

Table 3.13Output of df['Hindi'] -- the Hindi column's values
IndexHindi
020
122
224
324
425
523
615
717
821
922
1024