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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • 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.  Set Widths of Columns in Word Document Table with python-docx
  • 2.  Get Current Datetime in Python
  • 3.  Get Day of the Year with Python
  • 4.  How to Check if a String Contains Vowels in Python
  • 5.  Check if String Does Not Contain Substring in Python
  • 6.  Convert Integer to Bytes in Python
  • 7.  How to Check if Number is Power of 2 in Python
  • 8.  Using Python to Count Number of Lines in String
  • 9.  Format Numbers as Dollars in Python with format()
  • 10.  Convert Set to 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy