percentile

How to compute percentile for an external element with given array?

How to compute percentile for an external element with given array? Question: I’m looking for a percentile function that accepts an array and an element where it would return closest percentile of the element. Some examples percentile([1,2,3,4,5], 2) => 40% percentile([1,2,3,4,5], 2.5) => 40% percentile([1,2,3,4,5], 6) => 100% Does anything like this or similar exist …

Total answers: 2

How does pandas.qcut deal with remainder values?

How does pandas.qcut deal with remainder values? Question: game_num = range(1,102,1) player_name = [‘Fred’]*101 dict = {‘name’:player_name,’game_num’:game_num} df = pd.DataFrame(dict) df[‘percentile_bin’] = pd.qcut(df[‘game_num’],100,list(range(1,101))) Problem If I enter df.percentile_bin.nunique() I get 98 which indicates that 2 percentile bins are not populated. You can see for instance below, that game_num 2 is allocated to the 1st percentile_bin …

Total answers: 1

Find different percentile for every group in data frame

Find different percentile for every group in data frame Question: I have the date frame with the following structure: df = pd.DataFrame({‘GROUP_ID’: np.random.randint(1, 7, size=100), ‘VALUES’: np.random.randint(0, 50, size=100)}) df[‘THRESHOLD’] = df[‘GROUP_ID’]*5 df = df[[‘GROUP_ID’,’VALUES’,’THRESHOLD’]] df.sort_values(by=’GROUP_ID’, inplace=True) (this one is just for example) A column THRESHOLD is actually a percentile (in %) for every group. …

Total answers: 1

Convert array into percentiles

Convert array into percentiles Question: I have an array that I want to convert to percentiles. For example, say I have a normally distributed array: import numpy as np import matplotlib.pyplot as plt arr = np.random.normal(0, 1, 1000) plt.hist(arr) For each value in that array, I want to calculate the percentile of that value (e.g. …

Total answers: 5

How can I efficiently get [many] quartiles?

How can I efficiently get [many] quartiles? Question: I need to encode numerical values by ranges: low: 0, medium: 1, high: 2 , very high: 3. I’m doing it for quartiles. I have the following code: import pandas as pd import numpy as np def fun(df): table = df.copy() # pandas dataframe N = int(table.shape[0]) …

Total answers: 1

Weighted percentile using numpy

Weighted percentile using numpy Question: Is there a way to use the numpy.percentile function to compute weighted percentile? Or is anyone aware of an alternative python function to compute weighted percentile? thanks! Asked By: user308827 || Source Answers: Unfortunately, numpy doesn’t have built-in weighted functions for everything, but, you can always put something together. def …

Total answers: 12

Eliminating all data over a given percentile

Eliminating all data over a given percentile Question: I have a pandas DataFrame called data with a column called ms. I want to eliminate all the rows where data.ms is above the 95% percentile. For now, I’m doing this: limit = data.ms.describe(90)[‘95%’] valid_data = data[data[‘ms’] < limit] which works, but I want to generalize that …

Total answers: 3