Asra Khalid
Asra Khalid

Reputation: 177

How to apply assert function on column of a data frame?

I have a dataframe named df2. I'm trying to apply assert function on its column name pop95 and pdenpavg.

When I apply the function it gives me following error

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

Upvotes: 0

Views: 1039

Answers (1)

Anant Mittal
Anant Mittal

Reputation: 2113

Try this

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

Ideally, if there is no negative number, the sum(df2["pop95"]) should be 0, otherwise, it will be equal to the number to negative values in the column.

Upvotes: 1

Related Questions