Check if dataframe has a zero element

pandas check if value is in column
check if string is in pandas dataframe
pandas any
pandas check if all columns are zero
check if row contains value pandas
pandas or
pandas check if row exists in another dataframe
pandas count zeros in column

In a pandas dataframe, what is the quickest way to check if at least one element is 0? Imagine the data is :

Name   Asset  Revenue
A       10     20
B       0      21

I need to return true because at least one element is 0. One element across the dataframe, not one element per row/column

Maybe using any twice

df.eq(0).any().any()
Out[173]: True

Pandas : 4 Ways to check if a DataFrame is empty in Python , Return whether any element is True, potentially over an axis. element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-​empty). Whether each column contains at least one True element (the default). Check if a single element exists in DataFrame using in & not in operators. Dataframe class provides a member variable i.e DataFrame.values. It returns a numpy representation of all the values in dataframe. We can use the in & not in operators on these values to check if a given element exists or not. For example,

You can do in this way:

 0 in df.values

pandas.DataFrame.any, Additional keywords have no effect but might be accepted for compatibility with NumPy. Specify axis='columns' to check if row-wise values all return True. It appears that the accepted the definition of empty in this thread is a dataframe with zero rows only. But there is a distinction between an empty dataframe with zero rows and zero columns and an empty dataframe with zero rows and at least one column. In each case the length of the index is 0 and empty = True as shown here:

You can use isin:

df.isin([0]).any().any()

pandas.DataFrame.all, isupper() for each element of the Series/Index. If a string has zero characters, False is returned for that check. Returns:. I am trying to determine whether there is an entry in a Pandas column that has a particular value. I tried to do this with if x in df ['id']. I thought this was working, except when I fed it a value that I knew was not in the column 43 in df ['id'] it still returned True. When I subset to a data frame only containing entries matching the

pandas.Series.str.isupper, DataFrame.all() method checks whether all elements are True, potentially over an It returns True if all elements within a series or along a Dataframe axis are non-zero, **kwargs : Additional keywords have no effect but might be accepted for Specify axis='columns' to check if row-wise values all return True. if all values  In Python Pandas, what's the best way to check whether a DataFrame has one (or more) NaN values? I know about the function pd.isnan, but this returns a DataFrame of booleans for each element. This post right here doesn't exactly answer my question either.

Python, To check if DataFrame is empty in Pandas, use DataFrame.empty attribute. The attribute returns a boolean indicator if the DataFrame is empty or not. In the following example, we have initialized a DataFrame with some rows and then check if Pandas DataFrame - Add or Insert Row · Pandas DataFrame - Get first N rows  DataFrame.any(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs)¶. Return whether any element is True, potentially over an axis. Returns False unless there at least one element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-empty).

How to Check if Pandas DataFrame is Empty?, Test whether all element is true over requested Pandas axis within a series or along a Dataframe axis that is False or equivalent (e.g. zero or empty). kwargs, Additional keywords have no effect but might be accepted for  DataFrame.all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs)¶. Return whether all elements are True, potentially over an axis. Returns True unless there at least one element within a series or along a Dataframe axis that is False or equivalent (e.g. zero or empty).

Comments
  • I believe this would be the fastest. Great answer +1.
  • Be careful. This doesn't work for floats with 0 value.