Series.clip_upper() is used for
Series.clip_upper() values above the passed maximum value. A threshold value is passed as a parameter, and all values in the sequence that exceed the threshold values become equal to it.
Syntax: Series.clip_upper (threshold, axis = None, inplace = False)
threshold: numeric or list like, Sets maximum threshold value and in case of list, sets separate threshold values for each value in caller series (Given list size is same)
axis: 0 or ’index’ to apply method by rows and 1 or ’columns’ to apply by columns.
inplace: Make changes in the caller series itself. (Overwrite with new values)
Return type: Series with updated values
To load the dataset used in the following example, click here.
In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.
Example # 1 : Applied to single value series
In this example, a maximum threshold value of 26 is passed as a parameter to the .clip_upper () method. This method is called on the Age column of the data frame, and the new values are stored in the Age_new column. Before performing any operations, null lines are removed using .dropna ()
As shown in the output image, the maximum value of st of the Age_new column is 26. All values over 26 have been cropped to 26.
Example # 2: Applying to series with a list type value
In this example, the first 10 rows of the Age column are retrieved and stored using
.head () . After that, a list of the same length is created and passed to the
.clip_upper () parameter of the
.clip_upper () method to set a separate threshold value for each value in the series. The returned values are stored in a new column "clipped_values".
As shown in the output image and, each value in the sequence had a different threshold value according to the passed list, and therefore results were returned according to a separate threshold value for each item. All values above their respective thresholds have been clipped to the threshold.