Home > Software design >  Find a quantile value and then add it back to the dataframe
Find a quantile value and then add it back to the dataframe

Time:10-06

I have one dataframe that looks like this:

 ID | Name | pay
 1    John   10
 1    John   20
 1    John   30

I want to find the 50th percentile of pay and then add that value back to the orginal df.

df = np.round(users.groupby('ID')['pay'].quantile(0.50), 3).reset_index()

ID | Name | pay | 50th percentile
 1    John   10       20 
 1    John   20       20
 1    John   30       20

I've seen where you can use transform and pass it a function, but I haven't seen an example using the quantile function, so I'm not sure how to pass the quantile function to transform.

I thought something like this df = df.groupby('ID')['pay'].transform(quantile(0.50))

CodePudding user response:

You can add positional arguments to functions with groupby.transform

import pandas as pd
import io

t = '''
 ID   Name   pay
 1    John   10
 1    John   20
 1    John   30
 2    Doris  40
 2    Doris  100'''
df = pd.read_csv(io.StringIO(t), sep='\s ')

df['median'] = df.groupby('ID').pay.transform('quantile', .5)
df

Output

   ID   Name  pay  median
0   1   John   10    20.0
1   1   John   20    20.0
2   1   John   30    20.0
3   2  Doris   40    70.0
4   2  Doris  100    70.0
  • Related