Python 3.x 如何在数据帧的列上应用断言函数?
我有一个名为df2的数据帧。我正在尝试对其列名pop95和pdenpavg应用断言函数 当我应用函数时,它会给我以下错误Python 3.x 如何在数据帧的列上应用断言函数?,python-3.x,assert,Python 3.x,Assert,我有一个名为df2的数据帧。我正在尝试对其列名pop95和pdenpavg应用断言函数 当我应用函数时,它会给我以下错误 assert df2[(df2['pop95']>0)] --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <
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().
assert df2[(df2['pop95']>0)]
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
1#断言为负值
2.
---->3断言df2[(df2['pop95']>0)]
~\Anaconda3\lib\site packages\pandas\core\generic.py in\uuuu非零(self)
1553“a{0}的真值是不明确的。”
1554“使用a.empty、a.bool()、a.item()、a.any()或a.all()。”格式(
->1555自我类名称__
1556 )
1557 )
ValueError:数据帧的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
试试这个
assert sum(df2[“pop95”]您到底想做什么?我可以提供帮助。我有一个数据集(df2),在其中我应用了一个条件来删除列(pop95)中的负值。现在我必须使用assert来检查列(pop95)中是否没有剩余的负值。如何在数据集的特定列上应用assert?如果没有负值,输出结果会是什么?