How to Print values above 75th percentile from series Using Quantile using Pandas?
Given a series, the task is to print all the elements that are above the 75th percentile using Pandas in Python. There is a series of data, we have to find all the values of the series object whose value is greater than the 75th Percentile.
Approach:
- Create a series object of any dataset
- We will calculate 75th percentile using the quantile function of the pandas series
- We will apply for loop for iterating all the values of series object
- Inside for loop, we’ll check whether the value is greater than the 75th quantile value that is calculated in step(2) if greater then print it.
Code:
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np # Making an array arr = np.array([ 42 , 12 , 72 , 85 , 56 , 100 ]) # creating a series Ser1 = pd.Series(arr) # printing this series print (Ser1) # calculating quantile/percentile value quantile_value = Ser1.quantile(q = 0.75 ) # printing quantile/percentile value print ( "75th Percentile is:" , quantile_value) print ( "Values that are greater than 75th percentile are:" ) # Running a loop and # printing all elements that are above the # 75th percentile for val in Ser1: if (val > quantile_value): print (val) |
Output:
0 42 1 12 2 72 3 85 4 56 5 100 dtype: int32 75th Percentile is: 81.75 Values that are greater than 75th percentile are: 85 100
Explanation:
We have made a series object from an nd array and used the quantile() method to find the75% quantile or 75th percentile value of the data in the given series object and then use a for loop to find out all values of the series that are above the 75th percentile.
Please Login to comment...