search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Pandas DataFrame | clip method

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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.

0 or "index"

Clip element-wise for each column.

1 or "columns"

Clip element-wise for each row.

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]
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!