Skip to content

Instantly share code, notes, and snippets.

@ashishsingal1
Created April 27, 2015 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishsingal1/e1828ffd1a449513b8f8 to your computer and use it in GitHub Desktop.
Save ashishsingal1/e1828ffd1a449513b8f8 to your computer and use it in GitHub Desktop.
How to qcut with non unique bin edges
# I've had a lot of problems with creating unique bins for decile analysis,
# so I wrote this code that won't give you the "non unique bin error" in pandas
def calc_ranks(events, fields, result_field, cuts=10):
cut_size = cuts / 100.0
result = {}
for i in fields:
result[i] = {}
events[i+'_rank'] = events[i].rank(pct=True, ascending=True)
for j in range(cuts):
result[i][j] = events[(events[i+'_rank'] > j*cut_size) & (events[i+'_rank'] <= (j+1)*cut_size)][result_field].mean()
return pd.DataFrame(result)
@anuragreddygv323
Copy link

could you give an example ?

and some explanation about the parameters passed?

@52qingxin
Copy link

could you give an example ?

and some explanation about the parameters passed?

Copy link

ghost commented May 5, 2020

Hi Ashish,
Thanks for sharing this valuable code. May I know the parameters need to pass.
Otherwise this code is not useful at all.

Regards,
Bhagwat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment