Pandas DataFrame | any method
Start your free 7-days trial now!
Pandas any(~)
method scans through each row or column of the DataFrame, and if the row/column contains at least one non-zero value or a boolean True
, then True
is returned for that row/column.
In Pandas. a boolean True
is equivalent to a 1
, while a False
is equivalent to a 0
.
Parameters
1. axis
| int
or string
| optional
Whether or not to scan each row or column of the source DataFrame:
Axis | Description | Return type |
---|---|---|
| Scan each column |
|
| Scan each row |
|
| Scan the entire DataFrame |
|
By default, axis=0
.
2. bool_only
| boolean
| optional
If
True
, then only boolean columns will be scanned.If
False
, then all columns will be scanned.
By default, bool_only=False
.
3. skipna
| boolean
| optional
If
True
,NaN
will be treated asFalse
.If
False
, thenNaN
will be treated asTrue
.
By default, skipna=True
.
4. level
| int
or string
| optional
The level to target. This is only relevant if your DataFrame is a multi-index.
Return Value
The return type depends on the axis
:
If
axis=None
, then a single boolean is returned.Otherwise, a Series of boolean is returned.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,0,1], "B":[1,0,0], "C":[0,0,0]})df
A B C0 2 1 01 0 0 02 1 0 0
Scanning column-wise
To scan column-wise for the presence of non-zero values:
df.any() # or explicitly set axis=0
A TrueB TrueC Falsedtype: bool
Here, note the following:
the columns
A
andB
have at least one non-zero value in them, and soTrue
is returned for these columns.the return type is
Series
sinceaxis != None
.
Scanning row-wise
Just for your reference, we show df
here again:
df
A B C0 2 1 01 0 0 02 1 0 0
To scan row-wise, set axis=1
for the presence of non-zero values:
df.any(axis=1) # returns a Series
0 True1 False2 Truedtype: bool
Scanning entire DataFrame
To scan the entire DataFrame for the presence of non-zero values, set axis=None
:
df.any(axis=None)
True
Scanning boolean columns only
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,0], "B":[True,False], "C":[0,0]})df
A B C0 2 True 01 0 False 0
To scan through columns with booleans only, set bool_only=True
:
df.any(bool_only=True)
B Truedtype: bool
Notice how only column B
was considered since it is the only column that is of type boolean
.
Accounting for missing values
By default, skipna=True
, which means that missing values will be skipped (treated as 0
or False
).
As an example, consider the following DataFrame:
df = pd.DataFrame({"A": [pd.np.nan, 0]})df
A0 NaN1 0.0
Calling any()
gives:
df.any() # or skipna=True
A Falsedtype: bool
Here, we get False
because with the NaN
skipped, all values in column A
are 0
.
In contrast, setting skipna=False
gives treats NaN
as True
:
df.any(skipna=False)
A Truedtype: bool
As we now have at least one non-zero value in column A
, True
is returned.