Python | Pandas Series.ptp ()



Series.ptp() Pandas Series.ptp() returns the difference between the maximum value and
the minimum value in the object. This is the equivalent of the numpy.ndarray ptp method.

Syntax: Series.ptp (axis = None, skipna = None, level = None, numeric_only = None, ** kwargs)

Parameter:
axis: Axis for the function to be applied on.
skipna: Exclude NA / null values ​​when computing the result.
level: If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar.
numeric_only: Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
** kwargs: Additional keyword arguments to be passed to the function.

Returns: ptp: scalar or Series (if level specified)

Example # 1: Use Series.ptp () to return the difference between the maximum and minimum baseline values data in this Series object.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd. Series ([ 10 , 25 , 3 , 11 , 24 , 6 ])

  
# Create index

index_ = [ ` Coca Cola` , `Sprite` , ` Coke` , `Fanta` , ` Dew` , `ThumbsUp` ]

 
# set index

sr.index = index_

  
# Print series

print (sr)

Output:

Now we will use Series.ptp () to find the difference between the maximum and minimum values ​​in a given series object.

# return the difference between
# maximum and minimum value

result = sr.ptp ()

 
# Print the result

print (result)

Output:

As we can see in the output, Series.ptp () successfully returned the difference between the maximum and minimum values ​​of the underlying data in this Series object.

Example # 2: Use Series.ptp () to return the difference between the maximum and minimum values ​​of the underlying data in a given Series object .

Output:

Now we will use Series.ptp () to find the difference between maxim the minimum and minimum value in this series object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 11 , 21 , 8 , 18 , 65 , 84 , 32 , 10 , 5 , 24 , 32 ])

  
# Print series

print (sr)

# return the difference between
# maximum and minimum value

result = sr.ptp ()

 
# Print result

print (result)

Output:

As we can see in the output, Series.ptp () successfully returned the difference between the maximum and minimum values ​​of the underlying data in the given series object.

Example # 3: Use Series.ptp () to return the difference between the maximum and minimum values ​​of the underlying data in this Series object. This series object contains some missing values.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 19.5 , 16.8 , None , 22.78 , None , 20.124 , None , 18.1002 , None ])

 
# Print series

print (sr)

Output:

We will now use Series.ptp () to find the difference between the maximum and the minimum value in the given object of the series. we`re going to skip missing values ​​in the calculation.

# return the difference between
# maximum and minimum value

result = sr.ptp ( skipna = True )

 
# Print result

print (result)

Output:

As we see on output, Series.ptp () successfully returned the difference between the maximum and minimum values ​​of the underlying data in the given series object. < / p>