2016-08-24 14 views
2

私は.loc APIでを使用しない::どのように使用するには、*と*パンダ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 

してみてくださいしかし、私はこのエラーを取得する::

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(). 

今の私これを行う;(::

df.loc[df.age>30].loc[(df.age > df.aa2)] 

# aa2 age nom 
# 1 3 33 z 
# 4 23 33 p 
+1

http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing – ayhan

答えて

4
>>> df.loc[(df.age>30) & (df.age > df.aa2)] 
    aa2 age nom 
1 3 33 z 
4 23 33 p 
2

私は少しよりよい、より読みやすいquery()方法を好む行います

In [3]: df.query('age > 30 and age > aa2') 
Out[3]: 
    aa2 age nom 
1 3 33 z 
4 23 33 p 

PSがよく、それは(M.Klugerford has already shown you how to do this using .loc[] @)を直接あなたの質問に答えていないが、それはあなたに私の個人的な意見で(より良いを与えます)代替

関連する問題