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

Keep all rows before first occurrence of a value

I have a pandas dataframe and the opposite question to Drop all rows before first occurrence of a value

Instead of drop all rows before the first occurence of a value, I want to keep the rows before the first occurence of a value. so for example, i want to keep all the rows before the occurence 1 in the column count:

Year  ID Count
1997  1  0
1998  2  0
1999  3  1
2000  4  0
2001  5  1

leads to:

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

Year  ID Count
1997  1  0
1998  2  0

>Solution :

Use cummax and negate its output (~) to form a boolean Series for boolean indexing:

out = df[~df['Count'].eq(1).cummax()]

Output:

   Year  ID  Count
0  1997   1      0
1  1998   2      0

Intermediates:

   Year  ID  Count  eq(1)  cummax      ~
0  1997   1      0  False   False   True
1  1998   2      0  False   False   True
2  1999   3      1   True    True  False
3  2000   4      0  False    True  False
4  2001   5      1   True    True  False

Variant, if you are sure that there is at least one 1:

out = df.iloc[:np.argmax(df['Count'].eq(1))]
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