Change language

Python | Pandas Series.clip_upper ()

|

Pandas 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)

Parameters:
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 ()

# pandas module import

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 
# remove null values ​​to avoid errors

data.dropna (inplace = True

 
# setting the threshold

threshold = 26.0

 
# apply the method and go to the new column

data [ "Age_new" ] = data [ "Age" ]. clip_upper (threshold)

  
# display the top 10 lines

data.head ( 10 )

Output:
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".

# pandas module import

import pandas as pd 

 
# regex module import

import re

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering /wp-content/uploads/nba.csv"

 
 # remove null values ​​to avoid errors

data.dropna (inplace = True

 
# returns top lines

new_data = data.head ( 10 ). copy ()

 
# list for individual thresholds

threshold = [ 27 , 23 , 19 , 30 , 26 22 , 22 , 41 , 11 , 33 ]

  
# applying the method and returning to a new column

new_data [ "Clipped values" ] = new_data [ "Age" ]. clip_upper (threshold = threshold)

 
# display
new_data

Output:
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. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method