2017-09-05 51 views
0

我在其中包含布尔大熊猫有一列,并希望自上次真值来算THR行,像这样:计数行,因为条件

a   b 
False  0 
True  0 
False  1 
False  2 
False  3 
True  0 
False  1 
True  0 

我可以通过一个循环做到这一点,但它似乎有必须是更好的方式

回答

2
a = ~df['a'] 
b = a.cumsum() 
c = b-b.where(~a).ffill().fillna(1).astype(int) 
print (c) 
0 0 
1 0 
2 1 
3 2 
4 3 
5 0 
6 1 
7 0 
Name: a, dtype: int32