Reputation: 613
I need to split groups into consecutive, dense, integer-valued bins. This code is my attempt, but it's not working properly. It looks like the grouping is lost prior to ranking.
groups = [1] * 8 + [2] * 8
values = [11,12,13,11,12,13,11,12, 21,22,23,24,25,26,27,28]
series = pd.Series(values, index=groups)
grouped = series.groupby(series.index)
cut = grouped.apply(pd.cut, bins=4)
ranked = cut.rank(method="dense", ascending=True)
result = ranked.astype(int)
# This is the desired result:
assert list(result) == [1,2,3,1,2,3,1,2, 1,1,2,2,3,3,4,4]
Any good ideas how to do this correctly?
Upvotes: 1
Views: 346
Reputation: 323236
Change to transform
grouped.transform(lambda x : pd.cut(x, bins=4).factorize()[0]+1)
Upvotes: 3