• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / pandas mode – Find Mode of Series or Columns in DataFrame

pandas mode – Find Mode of Series or Columns in DataFrame

January 4, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Date Format YYYYMMDD in Python
  • 2.  Python Coin Flip – How to Simulate Flipping a Coin in Python
  • 3.  Comparing Datetimes in Python
  • 4.  Get the Count of NaN in pandas Using Python
  • 5.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 6.  Sort by Two Keys in Python
  • 7.  How to Count Vowels in a String Using Python
  • 8.  Using Python to Create List of Prime Numbers
  • 9.  Count Values by Key in Python Dictionary
  • 10.  Remove Empty Strings from List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy