GreenGodot
GreenGodot

Reputation: 6753

Conditional searches on pandas Dataframe

I'm trying to filter a Pandas dataframe. The format of the data is a block of data with whitespace in between.

data1, data2

val1,  val2
val1,  val2

val1,  val2

I want to filter the blocks but keep the whitespace in the dataframe. My code that should do this is:

df = df.loc[(df['data2'] >= threshold) and (df['data2'] == '')]

but this returns the error:

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

I've tried inserting these methods into my line, but nothing seems to work, it's probably my sleeop-deprived brain but if anyone

Upvotes: 1

Views: 2606

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

The use of and indeed causes the interpreter to think you're trying to find a boolean value using two types that don't support this. In this case, you're probably looking for &:

df['data2'] >= threshold) & (df['data2'] == '')

Example

In [1]: import numpy as np

In [2]: np.array([True, False]) & np.array([True, True])
Out[2]: array([ True, False], dtype=bool)

In [3]: np.array([True, False]) and np.array([True, True])
-------------------------------------------------------------------    --------
ValueError                                Traceback (most recent     call last)
<ipython-input-3-76eeded6cdad> in <module>()
----> 1 np.array([True, False]) and np.array([True, True])

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 2

Related Questions