Reputation: 7837
I try do use and in a .loc
API::
df = pd.DataFrame(dict(age=[99, 33, 33, 22, 33, 44],
aa2=[199, 3, 43, 22, 23, 54],
nom=['a', 'z', 'f', 'b', 'p', 'a'],))
df.loc[df.age>30]
# aa2 age nom
# 0 199 99 a
# 1 3 33 z
# 2 43 33 f
# 4 23 33 p
# 5 54 44 a
But I get this error::
df.loc[df.age>30 and (df.age > df.aa2)]
# ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last)
# <ipython-input-13-930dff789922> in <module>()
# ----> 1 df.loc[df.age>30 and (df.age > df.aa2)]
#
# /site-packages/pandas/core/generic.pyc in __nonzero__(self)
# 729 raise ValueError("The truth value of a {0} is ambiguous. "
# 730 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
# --> 731 .format(self.__class__.__name__))
# 732
# 733 __bool__ = __nonzero__
#
# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
For now I do this ;(::
df.loc[df.age>30].loc[(df.age > df.aa2)]
# aa2 age nom
# 1 3 33 z
# 4 23 33 p
Upvotes: 2
Views: 106
Reputation: 210852
I do prefer a bit nicer and more readable query() method:
In [3]: df.query('age > 30 and age > aa2')
Out[3]:
aa2 age nom
1 3 33 z
4 23 33 p
PS well, it doesn't answer your question directly (@M.Klugerford has already shown you how to do this using .loc[]
), but it gives you a better (in my personal opinion) alternative
Upvotes: 3
Reputation: 2647
>>> df.loc[(df.age>30) & (df.age > df.aa2)]
aa2 age nom
1 3 33 z
4 23 33 p
Upvotes: 3