To find the modes of the columns in a DataFrame, or the mode value of a Series in pandas, the easiest way is to use the pandas mode() function.
df.mode()
When working with data, many times we want to calculate summary statistics to understand our data better. One such statistic is the mode, or the value which occurs most for a given variable.
Finding the mode in a column, or the mode for all columns or rows in a DataFrame using pandas is easy. We can use the pandas mode() function to find the mode value of columns in a DataFrame.
The pandas mode() function works for both numeric and object dtypes.
Let’s say we have the following DataFrame.
df = pd.DataFrame({'Age': [43,23,43,49,71,37],
'Test_Score':[90,87,96,96,87,79]})
print(df)
# Output:
Age Test_Score
0 43 90
1 23 87
2 43 96
3 49 96
4 71 87
5 37 79
To get the modes for all columns, we can call the pandas mode() function.
print(df.mode())
# Output:
Age Test_Score
0 43.0 87
1 NaN 96
There is one mode for “Age” and two modes for “Test_Score”.
If we only want to get the mode of one column, we can do this using the pandas mode() function in the following Python code:
print(df["Test_Score"].mode())
# Output:
0 87
1 96
dtype: int64
Find the Mode of a Column with Object dtype in pandas
The mode() function works for both numeric and object dtypes.
Let’s say I have the following pandas DataFrame:
Name Weight_Change Month
0 Jim -16.20 1
1 Sally 12.81 1
2 Bob -20.45 1
3 Sue 15.35 1
4 Jill -12.43 1
5 Larry -18.52 1
6 Pam -6.10 2
7 Sally -2.81 2
8 Rose 12.45 2
9 Pat -0.32 2
10 Jill -1.23 2
11 Larry -8.52 2
12 Jim 5.20 3
13 Rob 12.81 3
14 Bob -2.45 3
15 Herman 5.35 3
16 Jill -2.43 3
17 Billy -1.85 3
We can use the mode() function to see who appears in our DataFrame the most by calling it on the “Name” column.
print(df["Name"].mode())
#Output:
0 Jill
dtype: object
Hopefully this article has been helpful for you to understand how to find the mode of a Series or DataFrame in pandas.