search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Pandas DataFrame | any method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Data Indexing and Masks
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

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.

NOTE

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

Series

0 or "index"

Scan each row

Series

1 or "columns"

None

Scan the entire DataFrame

Boolean

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 as False.

  • If False, then NaN will be treated as True.

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 C
0 2 1 0
1 0 0 0
2 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 True
B True
C False
dtype: bool

Here, note the following:

  • the columns A and B have at least one non-zero value in them, and so True is returned for these columns.

  • the return type is Series since axis != None.

Scanning row-wise

Just for your reference, we show df here again:

df
A B C
0 2 1 0
1 0 0 0
2 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 True
1 False
2 True
dtype: 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 C
0 2 True 0
1 0 False 0

To scan through columns with booleans only, set bool_only=True:

df.any(bool_only=True)
B True
dtype: 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
A
0 NaN
1 0.0

Calling any() gives:

df.any() # or skipna=True
A False
dtype: 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 True
dtype: bool

As we now have at least one non-zero value in column A, True is returned.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!