如何在数据框的列上应用断言函数?

时间:2019-11-06 11:18:45

标签: python-3.x assert

我有一个名为df2的数据框。我正在尝试在其列名pop95和pdenpavg上应用断言函数。

当我应用该功能时,会出现以下错误

assert df2[(df2['pop95']>0)]


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-133-4347947eb86c> in <module>
      1 #Asserting for negative value
      2 
----> 3 assert df2[(df2['pop95']>0)]

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1553             "The truth value of a {0} is ambiguous. "
   1554             "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
-> 1555                 self.__class__.__name__
   1556             )
   1557         )

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

1 个答案:

答案 0 :(得分:0)

尝试一下

assert sum(df2["pop95"]<0) == 0, "There are some negative values"

理想情况下,如果没有负数,则sum(df2 [“ pop95”])应该为0,否则,它将等于列中负数的数字。