Pandas DataFrame any() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd import numpy as np


The any() method evaluates each element to determine if the value is True/False on a specified axis. This method returns True if a DataFrame axis is Non-Zero or Non-Empty, else False returns.

The syntax for this method is as follows:

DataFrame.any(axis=0, bool_only=None, skipna=True, level=None, **kwargs)

Parameters Description
axis If zero (0) or index is selected, apply to each column. Default 0.If one (1) apply to each row.
bool_only Includes only Boolean DataFrame columns. If None, this parameter will attempt to use everything. Not supported for Series.
skipna This parameter excludes NaN/NULL values. If the row/column is NaN and skipna=True, the result is True. For an empty row/column and skipna=False, then NaN is treated as True because they are not equal to 0.
level If the axis is MultiLevel, count along with a specific level and collapse into a Series.
**kwargs Additional keywords have no effect.

For this example, Rivers Clothing assumes every item in their inventory contains a valid value. To confirm this, run the following code.

df_inv = pd.DataFrame({'Tops': [36, 23, 0], 'Tanks': [10, 20, 0], 'Pants': [61, 33, 0], 'Sweats': [88, 38, 0]})

index_ = ['Small', 'Medium', 'Large'] df_inv.index = index_

result = df_inv.any(axis='columns') print(result)

Output

There is an issue with the Large size of all items in inventory. They all contain zero values.

Small True
Medium True
Large False
dtype: bool

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.