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.
|
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.
|
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.
|
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.
|
Output:
Now we want to apply these restrictions to the data frame.
|
Output: