Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculating length of sequence of zeros in Pandas

I have a table like this

Unit status date
One 1 1
One 1 2
One 1 3
One 0 4
One 0 5
One 1 6
One 1 7

and I want to create a new column where I’d have the size of the sequence of zeros from the status column. So for that example, the output would be

Unit status date gap
One 1 1 0
One 1 2 0
One 1 3 0
One 0 4 2
One 0 5 2
One 1 6 0
One 1 7 0

This would have to be done for all the units in the DataFrame. I was basing myself on this question, but I’m stuck in the part where I set the total size for all the rows that are part of the gap

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The usual way to group the block of some values is to cumsum on the other values. Given that your data is sorted by Unit:

df['gap'] = (df.groupby(['Unit', 'status', df['status'].cumsum()])
             ['status'].transform('size')
             .where(df['status'].eq(0), other=0)
            )

Output:

  Unit  status  date  gap
0  One       1     1    0
1  One       1     2    0
2  One       1     3    0
3  One       0     4    2
4  One       0     5    2
5  One       1     6    0
6  One       1     7    0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading