JCV
JCV

Reputation: 515

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

Upvotes: 2

Views: 499

Answers (3)

Surjit Samra
Surjit Samra

Reputation: 4662

you could use logical_and

df_out = df[np.logical_and(df.A<=fence_high , df.A>=fence_low)]

Upvotes: 1

Guru Stron
Guru Stron

Reputation: 142173

You can negate between:

df_out = df[~df['A'].between(fence_low, fence_high, inclusive='neither')]

Upvotes: 1

BrokenBenchmark
BrokenBenchmark

Reputation: 19242

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

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

Upvotes: 1

Related Questions