What does the all() method do in pandas series?

PandasServer Side ProgrammingProgramming

The all() method in the pandas series is used to identify whether any False values are present in the pandas Series object or not. The typical output for this all method is boolean values (True or False).

It will return True if elements in the series object all are valid elements (i.e. Non-Zero values) otherwise, it will return False. This means the pandas series all() method checks for whether all elements are valid or not.

Example

import pandas as pd

series = pd.Series([1,2,3,0,4,5])

print(series)

#applying all function
print(series.all())

Explanation

Here we have created a pandas series object using a simple python list with one zero element. After that, we applied the all() method on the series object and the expected output will be False.

Output

0   1
1   2
2   3
3   0
4   4
5   5
dtype: int64

False

In the above block, there are two outputs one is the series object output and the other is a boolean value output from the all() method. The pandas series.all() method return True or False values based on the elements in the series object. In this example, there is a zero value located in index label 3 so that our output will be False.

Example

import pandas as pd

series = pd.Series([1,2,3,29,4,5])

print(series)

#applying all function
print(series.all())

Explanation

In this example, we have created a pandas series object using a simple python list with non-zero elements. Then applied the all() method to the series object.

Output

0   1
1   2
2   3
3  29
4   4
5   5
dtype: int64
True

The expected output for this example is True because there is no non-zero elements in our series object. And we can see the series object and series.all() function output in the above block.

Example

# importing the pandas package
import pandas as pd
import numpy as np

# creating Series
s = pd.Series([2,np.nan, 4, 7, 3])
print(s)

print(s.all())

Explanation

In this following example, we have created another series object with a nan element. And then applied the all() method. The result for this all() method on this series object will be True because NaN is not a zero value and the remaining all elements are also non-zero values.

print(np.nan != 0)
True

Output

0   2.0
1   NaN
2   4.0
3   7.0
4   3.0
dtype: float64

True

The first part of the above block is the series object displayed by using the print function, and the boolean value True is the output from the pandas series.all() method.

raja
Updated on 17-Nov-2021 08:42:17

Advertisements