DataFrame.clip() function in pandas

Overview:

  • Based on a given a pair of values one for the lower threshold and another for the upper threshold the clip() function of panda's DataFrame class trims the values present in a DataFrame instance.

 

Example:

import pandas as pd

 

matrix = [(20,21,22,23),

          (24,25,26,27),

          (28,29,30,31),

          (32,33,34,35)]

dataFrame = pd.DataFrame(data=matrix)

print("DataFrame:");

print(dataFrame);

clipped = dataFrame.clip(23,32)

print(clipped);

 

By using the inplace parameter values of the original DataFrame itself can be changed. The invocation will be dataFrame.clip(23,32,inplace=True), which will make the DataFrame elements to be trimmed in-place.

In this case the return value will be None.

Output:

    0   1   2   3

0  23  23  23  23

1  24  25  26  27

2  28  29  30  31

3  32  32  32  32

Based on the lower threshold value 23 and the upper threshold value 32, the clipped DataFrame instance in the output has all the first row elements as 23 and the last row elements as 32.


Copyright 2024 © pythontic.com