search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | clip method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Basic and Descriptive Statistics
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.clip(~) method is used to ensure that the values of the DataFrame are between a particular range.

Parameters

1. lowerlink | scalar or array_like | optional

The lower bound to clip. Values that are smaller than lower will be replaced by lower. You can ignore the lower bound by passing in None.

2. upperlink | scalar or array_like | optional

The upper bound to clip. Values that are larger than upper will be replaced by upper. You can ignore the upper bound by passing in None.

3. axislink | int or string | optional

Whether to perform clipping to each row or each column:

Axis

Description

None

Clip entire DataFrame.

Clip element-wise for each column.

0 or "index"

Clip element-wise for each row.

1 or "columns"

By default, axis=None. When you pass in an array for either lower or upper, you should set axis to either 0 or 1 depending on your needs. See examples below for clarification.

4. inplace | boolean | optional

Whether or not to directly modify the source DataFrame without creating a new one. By default, inplace=False, that is, a new DataFrame will be returned and the source DataFrame will be kept intact.

Return Value

A DataFrame with its values clipped.

Examples

Basic usage

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,4],"B":[6,8]})
df
   A  B
0  2  6
1  4  8

Clipping with lower and upper bounds set to 3 and 6 respectively:

df.clip(lower=3upper=6)
   A  B
0  3  6
1  4  6

Here, note the following:

  • any value in the DataFrame that is smaller than 3 will be replaced by the value 3.

  • any value that is larger than 6 will be replaced by the value 6.

  • at the end, the lowest value in the DataFrame should be 3, and the largest should be 6.

Passing in an array

Clipping each column

Consider the same DataFrame as above:

df
   A  B
0  2  6
1  4  8

We can pass in an array of bounds, like so:

df.clip(lower=[5,2], axis=0)
A B
0 5 6
1 4 8

Here, since axis=0, we are clipping each column element-wise. To break down what's happening:

Column A: [5 2] lower clips [2 4] = [5 4]
Column B: [5 2] lower clips [6 8] = [6 8]

We could also set a combination of an array and a number like so:

df.clip(lower=[5,2], upper=6, axis=0)
   A  B
0  5  6
1  4  6

To break down what's happening:

Column A: [5 2] lower clips [2 4] = [5 4] -> upper clipped to [5 4]
Column B: [5 2] lower clips [6 8] = [6 8] -> upper clipped to [6 6]

Clipping each row

Here's df again for your reference:

df
   A  B
0  2  6
1  4  8

Setting axis=1 clips each row element-wise:

df.clip(lower=[5,2], upper=6, axis=1)
   A  B
0  5  6
1  5  6

Again, here's the break down:

Column One: [5 2] lower clips [2 6] = [5 6] -> upper clipped to [5 6]
Column Two: [5 2] lower clips [4 8] = [5 8] -> upper clipped to [5 6]
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_down
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!