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:
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))]