Advertisement
jack06215

[pandas] Inverse quantile

Sep 26th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. from scipy.interpolate import interp1d
  4.  
  5. # set up a sample dataframe
  6. df = pd.DataFrame(np.random.uniform(0,1,(11)), columns=['a'])
  7. # sort it by the desired series and caculate the percentile
  8. sdf = df.sort_values('a').reset_index()
  9. sdf['b'] = sdf.index / float(len(sdf) - 1)
  10. # setup the interpolator using the value as the index
  11. interp = interp1d(sdf['a'], sdf['b'])
  12.  
  13. vals = df['a'].quantile(0.57)
  14. print(interp(vals))
  15. print(interp(df['a'].quantile(0.43)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement