How to get values outside an interval pandas DataFrame

Question:

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
Asked By: Jeniffer Barreto

||

Answers:

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

df[(df['A'] <= fence_low) | (df['A'] >= fence_high)]
Answered By: BrokenBenchmark

You can negate between:

df_out = df[~df['A'].between(fence_low, fence_high, inclusive='neither')]
Answered By: Guru Stron

you could use logical_and

df_out = df[np.logical_and(df.A<=fence_high , df.A>=fence_low)]
Answered By: Surjit Samra