user131983 user131983 - 3 months ago 40
Python Question

How to select rows in a DataFrame between two values, in Python Pandas?

I am trying to modify a DataFrame

to only contain rows for which the values in the column
are between 99 and 101 and trying to do this with the code below.

However, I get the error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

and I am wondering if there is a way to do this without using loops.

df = df[(99 <= df['closing_price'] <= 101)]


You should use () to group your boolean vector to remove ambiguity.

df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)]