Change language

Python | Pandas dataframe.clip ()

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.

# import pandas as pd

import pandas as pd

 
# Create a data frame using a dictionary

df = pd.DataFrame ({ < code class = "string"> "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

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.

# Clip in range (-4, 9)

df.clip ( - 4 , 9 )

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.

# import pandas as pd

import pandas as pd

 
# Create a data frame using dictionaries

 

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

  " B " : [ - 1 , - 4 , 6 , , 11 , 3 ],

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

 
# Print data frame
df

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.

# lower limit for each individual column element.

lower_limit = pd.Series ([ 1 , - 3 , 2 , 3 , - 2 , - 1 ])

 
# upper limit for each individual column element.

upper_limit = lower_limit + 5

 
# Print lower_limit
lower_limit

  
# Print upper_limit
upper_limit

Output:


Now we want to apply these restrictions to the data frame.

# applying different limit values ​​for each column item

df.clip (lower_limit, upper_limit, axis = 0 )

Output:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 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

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

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