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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically