Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas DataFrame any() Method

In this tutorial, we will learn DataFrame.any() method of python pandas. Pandas DataFrame.any() method is used to check whether any element is True over the axis and returns False unless there is at least one element in the specified object is True. It returns the Series or DataFrame.

The below shows the syntax of DataFrame.any() method.

Syntax

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

Parameters

axis: '0' represents the index and '1' represents the columns and it indicates which axis or axes should be reduced. If this parameter is '0', it reduces the index and returns a Series whose index is the original column labels. If this parameter is '0', it reduces the column and returns a Series whose index is the original index and if the parameter is None, then it reduces all axes and returns a scalar.

bool_only: bool(True or False), the default is None and it includes only the boolean columns. If this parameter is None, it will attempt to use everything, then use only boolean data.

skipna: bool(True or False), the default is True. It excludes all missing or null values.

level: It represents the int or level name, default None. If the axis is a MultiIndex (hierarchical), count along with a particular level, collapsing into a Series.

**kwargs: It represents any, the default None. The additional keywords have no effect but might be accepted for compatibility with NumPy.

Example 1: Checking DataFrame using DataFrame.any() Method

The below examples show how the DataFrame.any() method works. It returns True if any of the values in DataFrame are True otherwise it returns False. If there is more than one column, it will check column-wise.

import pandas as pd
df_1=pd.DataFrame([True,True])
df_2=pd.DataFrame([False,False])
df_3=pd.DataFrame([True,False])
df_4=pd.DataFrame([True,False],[True,False])
print(df_1.any())
print(df_2.any())
print(df_3.any())
print(df_4.any())

Once we run the program we will get the following output.


0 True
dtype: bool
0 False
dtype: bool
0 True
dtype: bool
0 True
dtype: bool

Example 2: Checking DataFrame Columns using DataFrame.any() Method

The below example shows how to check the objects of the DataFrame column-wise using the DataFrame.any() method. This method checks all the elements of the column in the DataFrame and returns True only if any of the elements match the condition otherwise it returns False.

import pandas as pd
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data) 
print(df)
print(any(df['B']>df['A']))
print(any(df['B']<df['A']))

Once we run the program we will get the following output.


A B
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
True
False

Example 3: Checking DataFrame columns using DataFrame.any() Method

The below is another example that shows how to check the objects of the DataFrame column-wise using the DataFrame.any() method. In this example change the elements of the DataFrame and check the output. This method checks all the elements of the column in the DataFrame and returns True only if any of the elements match the condition otherwise it returns False.

import pandas as pd
data = {'A':[1,2,3,4,5],'B':[0,1,8,2,3]}
df = pd.DataFrame(data) 
print(df)
print(any(df['B']>df['A']))
print(any(df['B']<df['A']))

Once we run the program we will get the following output.


A B
0 1 0
1 2 1
2 3 8
3 4 2
4 5 3
True
True

Conclusion

In this tutorial, we learned how to the DataFrame.any() method of the python pandas. We solved examples by applying this method on DataFrame and understood how the function works.