Pandas Series.all()
Pandas Series.all() Tutorial
In this tutorial, we'll explore the Series.all() method in Pandas, which is used to determine if all elements in a Pandas Series evaluate to True based on a specified condition, with examples.
The syntax of Series.all() is:
Series.all(axis=0, bool_only=None, skipna=True, level=None, **kwargs)where
| Parameter | Description |
|---|---|
axis | [Optional] The axis along which the all() method is applied. Default is 0 (along rows). |
bool_only | [Optional] If True, only boolean-type Series are considered. Default is None. |
skipna | [Optional] If True, NA values are skipped. Default is True. |
level | [Optional] For a multi-level index, this specifies the level to check. Default is None. |
The Series.all() method returns True if all elements in the Series are True based on the specified conditions; otherwise, it returns False.
Examples for Pandas Series.all() function
1. Check if all elements are True (boolean Series) using Series.all()
In this example, we'll use Series.all() to check if all elements in a boolean Series are True.
Python Program
import pandas as pd
# Create a boolean series
bool_series = pd.Series([True, True, True])
# Check if all elements are True
if bool_series.all():
print("All elements are True.")
else:
print("All elements are NOT True.")Output
All elements are True.Let us take a False element in the series, and rerun the program.
Python Program
import pandas as pd
# Create a boolean series
bool_series = pd.Series([True, True, False])
# Check if all elements are True
if bool_series.all():
print("All elements are True.")
else:
print("All elements are NOT True.")Output
All elements are NOT True.2. Check if all elements are greater than 10
In this example, we'll use Series.all() to check if all elements in the given Series are greater than 10.
Python Program
import pandas as pd
# Create a series
series = pd.Series([15, 20, 25, 30])
# Check if all elements are greater than 10
if (series > 10).all():
print("All elements are greater than 10.")
else:
print("All elements are not greater than 10.")Output
All elements are greater than 10.Let us change a few values to less than 10, and check the output.
Python Program
import pandas as pd
# Create a series
series = pd.Series([15, 2, 25, 3])
# Check if all elements are greater than 10
if (series > 10).all():
print("All elements are greater than 10.")
else:
print("All elements are not greater than 10.")Output
All elements are not greater than 10.Summary
In this tutorial, we've covered the Series.all() method in Pandas, which is useful for checking whether all elements in a Series satisfy certain conditions.