Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两列布尔值之间的条件滚动计数器_Python_Pandas_Count_Rolling Computation - Fatal编程技术网

Python 两列布尔值之间的条件滚动计数器

Python 两列布尔值之间的条件滚动计数器,python,pandas,count,rolling-computation,Python,Pandas,Count,Rolling Computation,提前感谢您帮助像我这样的python新手 我希望在“count”列中实现以下结果,而不使用愚蠢的slow for循环 我相信这是可能的矢量化。有什么建议吗 再次感谢 A B count 0 False False 0 1 True False 1 2 False False 1 3 True False 2 4 False True 1 #

提前感谢您帮助像我这样的python新手

我希望在“count”列中实现以下结果,而不使用愚蠢的slow for循环

我相信这是可能的矢量化。有什么建议吗

再次感谢

       A       B       count
    0  False   False     0
    1  True    False     1
    2  False   False     1 
    3  True    False     2
    4  False   True      1 # True value in the other column: reset the counter
    5  False   False     1
    6  True    False     1 # True value in the other column: reset the counter
    7  False   True      1 # True value in the other column: reset the counter
    8  False   True      2
    9  False   False     2
   10  False   True      3
您可以尝试:

df['count'] = (df.groupby(df.A.cumsum())['B'].cumsum() + df.groupby(df.B.cumsum())['A'].cumsum())
输出:

        A      B  count
0   False  False      0
1    True  False      1
2   False  False      1
3    True  False      2
4   False   True      1
5   False  False      1
6    True  False      1
7   False   True      1
8   False   True      2
9   False  False      2
10  False   True      3

绝对正确快速的回答。非常感谢你!你是怎么想的这么快?