Counting the amount of times a boolean goes from True to False in a column
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Question
00:59 Accepted answer (Score 7)
01:55 Answer 2 (Score 4)
02:21 Answer 3 (Score 2)
02:35 Answer 4 (Score 1)
02:50 Thank you
--
Full question
https://stackoverflow.com/questions/5421...
Accepted answer links:
[bitwise and]: https://wiki.python.org/moin/BitwiseOper...
[pd.shift]: https://pandas.pydata.org/pandas-docs/st...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #series
#avk47
ACCEPTED ANSWER
Score 7
You can perform a bitwise and of the Col1 with a mask indicating where changes occur in successive rows:
(df.Col1 & (df.Col1 != df.Col1.shift(1))).sum()
3
Where the mask, is obtained by comparing Col1 with a shifted version of itself (pd.shift):
df.Col1 != df.Col1.shift(1)
0      True
1     False
2     False
3      True
4     False
5     False
6      True
7     False
8     False
9     False
10     True
11    False
12    False
13     True
14    False
15    False
16    False
17    False
Name: Col1, dtype: bool
For multiple columns, you can do exactly the same (Here I tested with a col2 identical to col1)
(df & (df != df.shift(1))).sum()
Col1    3
Col2    3
dtype: int64
ANSWER 2
Score 4
Notice that subtracting True (1) from False (0) in integer terms gives -1:
res = df['Col1'].astype(int).diff().eq(-1).sum()  # 3
To apply across a Boolean dataframe, you can construct a series mapping label to count:
res = df.astype(int).diff().eq(-1).sum()
ANSWER 3
Score 2
Just provide different idea
df.cumsum()[~df.Col1].nunique()
Out[408]: 
Col1    3
dtype: int64
ANSWER 4
Score 1
My strategy was to find where the difference in one row to the next. (Considering that Trues are 1's and Falses are 0's, of course.)
Thus, Colm1 - Colm1.shift() represents the Delta value where a 1 is a shift from False to True, 0 No Change, and -1 shift from True to False.
import pandas as pd
d = {'Col1': [True, True, True, False, False, False, True, True, True, True, False, False, False, True, True, False, False, True, ]}
df = pd.DataFrame(data=d)
df['delta'] = df['Col1'] - df['Col1'].shift()
BooleanShifts = df['delta'].value_counts()
print(BooleanShifts[-1])
After getting the value counts as a dict of these [1, 0, -1] values, you can select for just the -1's and get the number of times the DF shifted to a False Value from a True Value. I hope this helped answer your question!