Alex
Alex

Reputation: 1131

Pandas: Get percentile value by specific rows

i try to get the percentile of the value in column value, based on min and max column

import pandas as pd
d = {'value': [20, 10, -5, ],
     'min': [0, 10, -10,],
    'max': [40, 20, 0]}
df = pd.DataFrame(data=d)
df

I obtain a new column "percentile", which looks like this:

d = {'value': [20, 10, -5, ],
 'min': [0, 10, -10,],
'max': [40, 20, 0],
'percentile':[50, 0, 50]}
df_res = pd.DataFrame(data=d)
df_res

Thanks

Upvotes: 0

Views: 77

Answers (1)

Hugolmn
Hugolmn

Reputation: 1560

You need to offset the min so that it becomes a ratio between value and max. You can do :

df['percentile'] = (df['value'] - df['min']) / (df['max'] - df['min']) * 100

Upvotes: 1

Related Questions