The all()
method in Pandas is used to check if all elements in a DataFrame satisfy a certain condition.
Example
import pandas as pd
data = {'A': [True, True, True, True],
'B': [True, False, True, True]}
df = pd.DataFrame(data)
# check if all values in column 'A' are True
result = df['A'].all()
print(result)
# Output: True
all() Syntax
The syntax of the all()
method in Pandas is:
df.all(axis=0, bool_only=None, skipna=True, **kwargs)
all() Arguments
The all()
method takes following arguments:
axis
(optional) - specifies the axis along which the operation should be appliedskipna
(optional) - determines whether to skip missing values when evaluating the condition**kwargs
(optional) - additional keyword arguments that can be passed to the function
all() Return Value
The all()
method returns a single boolean value, indicating whether all the elements meet the specified condition.
Example1: Use all() to Check if All Values in a Column Meet a Condition
import pandas as pd
# create a DataFrame
data = {'A': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
# check if all values in column A are greater than 0
condition = (df['A'] > 0).all()
if condition:
print("All values in column 'A' are greater than 0.")
else:
print("Not all values in column 'A' are greater than 0.")
Output
All values in column 'A' are greater than 0.
In the above example, we first created the df DataFrame with the column named A
.
Then, we used the all()
method along with the condition df['A'] > 0
to check if all values in column A
are greater than 0.
Since all the values satisfy the condition, the all()
method returns True.
Example 2: Check Conditions With all() Along Different Axes
import pandas as pd
data = {'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)
# check if all elements in each column are greater than 2
result_axis_0 = (df > 2).all(axis=0)
print("All elements in each column greater than 2:")
print(result_axis_0)
print()
# check if all elements in each row are greater than 2
result_axis_1 = (df > 2).all(axis=1)
print("\nAll elements in each row greater than 2:")
print(result_axis_1)
Output
All elements in each column greater than 2: A False B True dtype: bool All elements in each row greater than 2: 0 False 1 False 2 True 3 True dtype: bool
Here, we used all()
to check if all elements in each column are greater than 2 using axis=0
, and if all elements in each row are greater than 2 using axis=1
respectively.
Example 3: Effect of skipna Argument in all()
import pandas as pd
data = {'A': [True, True, None, True],
'B': [True, None, True, True]}
df = pd.DataFrame(data)
# check if all values in column A are True, without skipping missing values
result_skipna_false = df['A'].all(skipna=False)
print("Without skipping NA values:", result_skipna_false)
# check if all values in column B are True, with skipping missing values
result_skipna_true = df['B'].all(skipna=True)
print("Skipping NA values:", result_skipna_true)
Output
Without skipping NA values: False Skipping NA values: True
In this example,
- With
skipna=False
, we are checking if all values in columnA
areTrue
, without ignoring missing values. - With
skipna=True
(default behavior), we are checking if all non-missing values in columnB
areTrue
, effectively ignoring missing values.