2021年2月10日星期三

How to assign unique grouping value for each consecutive sequence of True values in pandas boolean mask

I am trying to generate an appropriate pandas groupBy

Say I have a boolean mask like so [false, false, true, false, true, true, false, true, true]

I would like the groupings to be like so [0,0,1,0,2,2,0,3,3]

I can certainly create this array via a loop through the mask but I would like if possible to use the pandas or numpy builtins for ease of use and perhaps vectorization.

(If no builtin exists I would appreciate a more pythonic way of doing this than via a straight loop with a state flag and rank counter)

https://stackoverflow.com/questions/66146049/how-to-assign-unique-grouping-value-for-each-consecutive-sequence-of-true-values February 11, 2021 at 06:28AM

没有评论:

发表评论