Search code examples
pythonpandasdataframefilteringapply

How to get values outside an interval pandas DataFrame


I'm trying to get the values outside an interval in pandas dataframe, and I was trying to avoid iterating over the rows. is there any way to do that?

This is what I was trying, but it gives the error

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

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
fence_low = 30
fence_high = 70
df_out = df[(df['A'] <= fence_low) or (df['A'] >= fence_high)]
df_out

Solution

  • I think you want to use the bitwise-OR operator rather than the or keyword:

    df[(df['A'] <= fence_low) | (df['A'] >= fence_high)]