Search code examples
pythonpandaslambdapandas-loc

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


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.


Solution

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