1

I'm trying to use Pandas' where method but I stuck with an error. Here is a very small example.

Give the file

Chamber,Treatment
1,Sem palha
1,Sem palha
1,Sem palha
2,Sem palha
2,Sem palha

when I run

import pandas
sample = pandas.read_csv('sample.csv')
sample.where(sample['Chamber'] == 1)

I get

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-62-96ed02cb41da> in <module>()
----> 1 sample.where(sample['Chamber'] == 1)

/usr/lib/python3.4/site-packages/pandas/core/generic.py in where(self, cond, other, inplace, axis, level, try_cast, raise_on_error)
   3346             new_data = self._data.where(other=other, cond=cond, align=axis is None,
   3347                                         raise_on_error=raise_on_error,
-> 3348                                         try_cast=try_cast)
   3349 
   3350             return self._constructor(new_data).__finalize__(self)

/usr/lib/python3.4/site-packages/pandas/core/internals.py in where(self, **kwargs)
   2434 
   2435     def where(self, **kwargs):
-> 2436         return self.apply('where', **kwargs)
   2437 
   2438     def eval(self, **kwargs):

/usr/lib/python3.4/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, **kwargs)
   2416                                                  copy=align_copy)
   2417 
-> 2418             applied = getattr(b, f)(**kwargs)
   2419 
   2420             if isinstance(applied, list):

/usr/lib/python3.4/site-packages/pandas/core/internals.py in where(self, other, cond, align, raise_on_error, try_cast)
   1043         axis = cond.ndim - 1
   1044         cond = cond.swapaxes(axis, 0)
-> 1045         mask = np.array([cond[i].all() for i in range(cond.shape[0])],
   1046                         dtype=bool)
   1047 

/usr/lib/python3.4/site-packages/pandas/core/internals.py in <listcomp>(.0)
   1043         axis = cond.ndim - 1
   1044         cond = cond.swapaxes(axis, 0)
-> 1045         mask = np.array([cond[i].all() for i in range(cond.shape[0])],
   1046                         dtype=bool)
   1047 

AttributeError: 'float' object has no attribute 'all'
1
  • sample.where expects a boolean array or NDFrame of the same shape as sample. What is the desired result? Did you want sample['Chamber'].where(sample['Chamber'] == 1) for example?
    – unutbu
    Nov 17, 2014 at 13:41

2 Answers 2

1

Your syntax would actually work in new versions of pandas. You can upgrade pandas on Ubuntu using

sudo pip install --upgrade pandas

Or if you are using python3, then

sudo pip3 install --upgrade pandas
0

As unutbu pointed out in the comment, where seems to be a "cell-wise" selection. These to variants work as expected:

sample[sample["Chamber"] == 1]

and

sample.query("Chamber == 1")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.