IndigoChild
IndigoChild

Reputation: 844

Obtaining index based on connditions

From the following code:

aps1.Status.head(10)
Out[663]: 
0    OK
1    OK
2    OK
3    OK
4    OK
5    OK
6    Fail
7    OK
8    Fail
9    OK

How to obtain the indexes for which Status is Fail? I tried:

 print (index for index,value in enumerate(aps1.Status) if value == "Fail"])

But I'm getting syntax error. Thanks

Upvotes: 0

Views: 43

Answers (4)

use a list comprehension with a iterrows on the dataframe with an if condition for Status column

status=['OK'
,'OK'
,'OK'
,'OK'
,'OK'
,'OK'
,'Fail'
,'OK'
,'Fail'
,'OK']
df=pd.DataFrame({'Status':status})
df.reset_index(inplace=True)

print([index for index,item in df.iterrows() if item['Status']=='Fail'])

output [6, 8]

Upvotes: 0

mgracer
mgracer

Reputation: 175

You are getting that error because of the extra ] . Try deleting it

Upvotes: 1

dbenarfa
dbenarfa

Reputation: 731

remove the ']' at the end

print (index for index,value in enumerate(aps1.Status) if value == "Fail"**]**)

Upvotes: 2

jezrael
jezrael

Reputation: 862406

Use boolean indexing with index:

L = aps1.index[aps1.Status == 'Fail']

Upvotes: 2

Related Questions