Python | Pandas dataframe.clip ()



Pandas dataframe.clip() is used to trim values ​​at a specified input threshold. We can use this function to set the lower and upper limits on the values ​​that any cell in a data frame can have.

Syntax: DataFrame.clip (lower = None, upper = None, axis = None, inplace = False, * args, ** kwargs)

Parameters:
lower: Minimum threshold value … All values ​​below this threshold will be set to it.
upper: Maximum threshold value. All values ​​above this threshold will be set to it.
axis: Align object with lower and upper along the given axis.
inplace: Whether to perform the operation in place on the data.
* args, ** kwargs: Additional keywords have no effect but might be accepted for compatibility with numpy.

Example # 1: Use the clip () function to clip data frame values ​​below and above a specified threshold.

# import pandas as pd

import pandas as pd

 
# Create a data frame using a dictionary

df = pd.DataFrame ({ < code class = "string"> “A” : [ - 5 , 8 , 12 , - 9 , 5 , 3 ],

"B" : [ - 1 , - 4 , 6 , 4 , 11 , 3 ],

  " C " : [ 11 , 4 , - 8 , 7 , 3 , - 2 ]})

 
# Print data frame for rendering
df

Now trim all values ​​from -4 to -4 and all values ​​from 9 to 9. Values ​​in the range from -4 to 9 remain the same.

# Clip in range (-4, 9)

df.clip ( - 4 , 9 )

Output:

Note that there are no values ​​greater than 9 or less than -4 in the data frame

Example # 2: Use the clip () function for clips, using specific low and high thresholds for each column item in the data frame.

# import pandas as pd

import pandas as pd

 
# Create a data frame using dictionaries

 

df = pd.DataFrame ({ "A" : [ - 5 , 8 , 12 , - 9 , 5 , 3 ],

  " B " : [ - 1 , - 4 , 6 , , 11 , 3 ],

"C " : [ 11 , 4 , - 8 , 7 , 3 , - 2 ]})

 
# Print data frame
df

when axis = 0 then the value will be truncated line by line. We`re going to provide a high and low threshold for all column items (i.e. the row count equivalent)

Create a series to store the low and high threshold for each column item.

# lower limit for each individual column element.

lower_limit = pd.Series ([ 1 , - 3 , 2 , 3 , - 2 , - 1 ])

 
# upper limit for each individual column element.

upper_limit = lower_limit + 5

 
# Print lower_limit
lower_limit

  
# Print upper_limit
upper_limit

Output:


Now we want to apply these restrictions to the data frame.

# applying different limit values ​​for each column item

df.clip (lower_limit, upper_limit, axis = 0 )

Output: