• 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
  • Ruby
  • About
You are here: Home / Python / pandas percentile – Calculate Percentiles of Series or Columns in DataFrame

pandas percentile – Calculate Percentiles of Series or Columns in DataFrame

January 4, 2022 Leave a Comment

To find percentiles of a numeric column in a DataFrame, or the percentiles of a Series in pandas, the easiest way is to use the pandas quantile() function.

df.quantile(0.25)

You can also use the numpy percentile() function.

np.percentile(df["Column"], 25)

When working with data, many times we want to calculate summary statistics to understand our data better. Percentiles, or quantiles, are very important for us to understand how the data is distributed.

Finding the percentile for a given column, or the quantile for all columns or rows in a DataFrame using pandas is easy. We can use the pandas quantile() function to find various quantile values of a column of numbers, or a DataFrame.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

To get the the 50th quantile, or the median, for all columns, we can call the pandas quantile() function and pass 0.5.

print(df.quantile(0.5))

# Output:
Age           46.0
Test_Score    88.5
Name: 0.5, dtype: float64

If we only want to get the percentile of one column, we can do this using the pandas quantile() function in the following Python code:

print(df["Test_Score"].quantile(0.5))

# Output:
88.5

Calculating Multiple Percentiles at Once with pandas

We can use the pandas quantile() function to calculate multiple percentiles at once. To calculate multiple quantiles, we pass a list of quantile values to the quantile() function.

Let’s say we have the same data from above. Let’s calculate the 25th, 50th and 75th percentiles of our data.

print(df.quantile([0.25,0.5,0.75]))

# Output:
        Age  Test_Score
0.25  38.50       84.75
0.50  46.00       88.50
0.75  51.25       91.50

Using numpy percentile to Calculate Medians in pandas DataFrame

We can also use the numpy percentile() function to calculate percentile values for the columns in our pandas DataFrames.

Let’s get the 25th, 50th, and 75th percentiles of the “Test_Score” column using the numpy percentile() function. We can do this easily in the following Python code. The difference here is that you need to pass integer values instead of decimal values (i.e. 50 instead of 0.50).

print(np.percentile(df["Test_Score"],[25,50,75]))

# Output:
[84.75 88.5  91.5]

As you can see above, this is the same value we received from the pandas quantile() function.

Hopefully this article has been helpful for you to understand how to find percentiles of numbers in a Series or DataFrame in pandas.

Other Articles You'll Also Like:

  • 1.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 2.  Get First Digit in Number Using Python
  • 3.  Using Python to Sort Two Lists Together
  • 4.  Calculate Sum of Dictionary Values in Python
  • 5.  Get Last Character in String in Python
  • 6.  Convert True to 1 in Python
  • 7.  Using Python to Replace Backslash in String
  • 8.  Print Time Elapsed in Python
  • 9.  Python max float – What’s the Maximum Float Value in Python?
  • 10.  Get Day of Week from Datetime in pandas DataFrame

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