How to check the data present in a Series object is monotonically decreasing or not?

PandasServer Side ProgrammingProgramming

To check if the data present in the series is monotonically decreasing or not, we can use the is_monotonic_decreasing property of the pandas Series constructor.

The monotonically decreasing data is nothing but continuously decreasing values. And the attribute “is_monotonic_decreasing” is used to verify that the data in a given series object is always decreasing or not. And this attribute returns a boolean value as an output.

Example 1

import pandas as pd

# create a series
s = pd.Series([100,57,23,10,5])

print(s)

print("Is monotonically decreasing: ", s.is_monotonic_decreasing)

Explanation

Here, we initialized a Series with a python list of integer values of length 5. Then we applied the is_monotonic_decreasing property on our series object “s”.

Output

0 100
1  57
2  23
3  10
4   5
dtype: int64

Is monotonically decreasing: True

In the above output, we can see the initial series object, and also the output of the is_monotonic_decreasing attribute.

For the following example, The is_monotonic_decreasing attribute returns True. which means the data in the given series is continuously decreasing.

Example 2

import pandas as pd

# create a series
s = pd.Series(list("abcdef"))

print(s)

print("Is monotonically decreasing: ", s.is_monotonic_decreasing)

Explanation

Let’s take another example for checking if the values in a series object is monotonically decreasing or not. Here, we have initialized a series object with a python list of strings. After that, we applied the is_monotonic_decreasing property over the data of the series object “s”.

Output

0 a
1 b
2 c
3 d
4 e
5 f
dtype: object

Is monotonically decreasing: False

We got the boolean value “False” as a result of the following example. which means that the values present in the given series object is not decreasing continuously.

raja
Updated on 09-Mar-2022 06:27:22

Advertisements