Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在API中使用*和*_Python_Pandas - Fatal编程技术网

Python 如何在API中使用*和*

Python 如何在API中使用*和*,python,pandas,Python,Pandas,我尝试在.locAPI:中使用and: 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

我尝试在
.loc
API:中使用and:

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

我更喜欢一种更好、可读性更强的方法:

PS好吧,它不会直接回答你的问题(@M.Klugerford),但它给了你一个更好的选择(在我个人看来)

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

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