Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use loc in pandas with more than one condition? [duplicate]

I'm looking at this example from the documentation:

df.loc[lambda df: df['shield']==8]

How do I extend this with an OR condition? I'm trying to do this but it doesn't work:

df.loc[lambda df: df['shield']==8 or df['max_speed'] ==1]

Also, as a side note, what is the point of the lambda function in this case since:

 df.loc[df['shield']==8]

works just fine.

like image 550
Noobcoder Avatar asked Nov 19 '25 09:11

Noobcoder


1 Answers

Because working with arrays, here Series is necessary use butwise OR by | and because priority of operators is necessary add parentheses:

df.loc[lambda df: (df['shield']==8) | (df['max_speed'] ==1)]
like image 78
jezrael Avatar answered Nov 20 '25 21:11

jezrael