1

I have a Pandas Dataframe that looks as follows:

    streak 
0      1.0 
1      2.0 
2      0.0 
3      1.0 
4      2.0 
5      0.0 
6      0.0 

I want to delete every row after the first 0.0 in the streak column.

The result should look like this:

    streak 
0      1.0 
1      2.0 

2 Answers 2

3

Get index of first 0 by idxmax and slice by iloc, only need default unique indices:

#df = df.reset_index(drop=True)
df = df.iloc[:df['streak'].eq(0).idxmax()]
print (df)
   streak
0     1.0
1     2.0

Detail:

print (df['streak'].eq(0).idxmax())
2

EDIT: For more general solution is necessary use numpy - get position by numpy.argmax:

print (df)
   streak
a     1.0
b     2.0
c     0.0
d     1.0
e     2.0
f     0.0
g     0.0

df = df.iloc[:df['streak'].eq(0).values.argmax()]
print (df)
   streak
a     1.0
b     2.0
Sign up to request clarification or add additional context in comments.

Comments

0

This is a generalized solution via numpy.where. It will work for matching first instance of any specified value.

df = df.iloc[:np.where(df['streak']==0)[0][0]]

#    streak
# 0     1.0
# 1     2.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.