Change language

Python | Pandas dataframe.clip_upper ()

|

Pandas dataframe.clip_upper() is used to trim values ​​at a specified input threshold. We use this function to trim all values ​​above the input threshold to the specified input.

Syntax: DataFrame.clip_upper (threshold, axis = None, inplace = False)

Parameters:
threshold: float or array_like
float : every value is compared to threshold.
array-like : The shape of threshold should match the object it’s compared to. When self is a Series, threshold should be the length. When self is a DataFrame, threshold should 2-D and the same shape as self for axis = None, or 1-D and the same length as the axis being compared.
axis: Align object with threshold along the given axis.
inplace: Whether to perform the operation in place on the data.

Returns: clipped: same type as input

Example # 1: Use clip_upper () to trim dataframe values ​​above a given threshold.

Now trim all values ​​above 8 to 8.

# import pandas as pd

import pandas as pd

  
# Create a data frame using a dictionary

df = pd.DataFra me ({ "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

# Trim all values ​​below 2

  df.clip_upper ( 8 )

Output:

Example # 2: Use clip_upper () to clip_upper () values ​​in the data frame with a specific value for each cell of the data frame.

For this purpose we can use an empty array, but the shape of the array must be the same as that of the information frame.

# pandas import as pd

import pandas as pd

 
# Create a data frame using a dictionary

 

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

"B" : [ - 1 , - 4 , 6 , 4 , 11 , < / code> 3 ],

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

 
# upper limit for each an individual column element.

limit = np .array ([[ 10 , 2 , 8 ], [ < code class = "value"> 3 , 5 , 3 ], [ 2 , 4 , 6 ],

  [ 11 , 2 , 3 ], [ 5 , 2 , 3 ], [ 4 , 5 , 3 ]])

  
# Print upper_limit
limit

Now apply these restrictions to the data frame.

# apply a different limit
# for each cell in the data frame
df.clip_upper (limit)

Output:

Each cell value has been cropped based on the corresponding upper applied limit.

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