I have a multi-index data frame with levels 'id' and 'year':
id year
1 2001 True
2002 True
2 2001 False
2002 True
3 2001 True
2002 True
I want to check, for each id level, if the boolean is True
for consecutive year and then count the number of times this happens. In the above case since for id 1 and id 3 this condition is satisfied then the count would be 2.
I tried to generate a dummy like this (in order to use np.count_nonzero(01_02)
afterwards) but this is obviously wrong:
01_02 = (df[df.index.get_level_values(1) == 2001]) & (df[
df.index.get_level_values(1) == 2002])
Notice that if you unstack the
id
index level ofdf
then you get:And we can think of the values above as a boolean array,
arr
:Imagine taking all the rows of the array except the last one:
and comparing it to all the rows of the array except the first one:
We want to count in how many locations they are equal and also equal to True: