Answers for "pandas count consecutive values"

-2

pandas count consecutive values

why the obsession with the ultra-pythonic way of doing things? readability + efficiency trumps "leet hackerz style."

I'd just do it like so:

a = [0,0,1,1,1,0,0,1,0,1,1]
b = [0,0,0,0,0,0,0,0,0,0,0]

for i in range(len(a)):
    if a[i] == 1:
        b[i] = b[i-1] + 1
    else:
        b[i] = 0
Posted by: Guest on March-12-2021

Python Answers by Framework

Browse Popular Code Answers by Language