Pandas DataFrame all() Method

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

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

Syntax

The syntax required to use these methods are as follows

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

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, the default value is 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: Checking DataFrame using DataFrame.all() Method

The below examples show how the DataFrame.all() method works. It returns True if all the values 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.all())
print(df_2.all())
print(df_3.all())
print(df_4.all())

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


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

Example: Checking DataFrame columns using DataFrame.all() Method

The below example shows how to check the objects of the DataFrame column-wise using the DataFrame.all() method. This function checks all the elements of the column in the DataFrame and returns True only if all 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(all(df['B']>df['A']))
print(all(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: Checking DataFrame columns using DataFrame.all() Method

This example is similar to the previous example. In the code just change any one element in the DataFrame and check the output.

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

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


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

Conclusion

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