Python | Pandas Index.any()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Index.any() function checks if any of the elements in the index are true or not. It returns one single boolean value if axis is not specified. It returns true if any one value in the index is true. It returns false if none of the values in the index is true.

Note : It treats 0 as false value.



Syntax: Index.any(*args, **kwargs)

Parameters :
*args : These parameters will be passed to numpy.any
**kwargs : These parameters will be passed to numpy.any

Returns : any : bool or array_like (if axis is specified)
A single element array_like may be converted to bool.

Example #1: Use Index.any() function to check if all values are true in the index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Index
df = pd.Index([17, 69, 33, 5, 0, 74, 0])
  
# Print the dataframe
df

chevron_right


Output :

Let’s check if any of the values in the index is true or it is having all the false value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# to check if there is any false 
# value present in the index
df.any()

chevron_right


Output :

As we can see in the output, the function has returned true indicating that there is at least one true value present in the Index.
 
Example #2: Use Index.any() function to check if any value in the index is true or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Index
df = pd.Index([0, 0, 0, 0, 0])
  
# Print the dataframe
df

chevron_right


Output :

Let’s check if any of the values in the index is true or it is having all the false value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# to check if there is any false
#  value present in the index
df.any()

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.