Pandas Tutorial Pandas References

Pandas Series - clip() function



The Pandas Series clip() function trims values at input threshold(s). It assigns values outside boundary to boundary values. Thresholds can be singular values or array like, and in the latter case the clipping is performed element-wise in the specified axis.

Syntax

Series.clip(lower=None, upper=None, 
            axis=None, inplace=False)

Parameters

lower Optional. Specify minimum threshold value as float or array-like. All values below this threshold will be set to it. A missing threshold (e.g NA) will not clip the value. Default: None
upper Optional. Specify maximum threshold value as float or array-like. All values above this threshold will be set to it. A missing threshold (e.g NA) will not clip the value. Default: None
axis Optional. Specify int or str axis name to align object with lower and upper along the given axis. Default: None
inplace Optional. If True, the operation is performed in place on the data. Default: False

Return Value

Returns same type as calling object with the values outside the clip boundaries replaced or None if inplace=True.

Example: using clip() on a Series

In the example below, the clip() function is used to clip the given series according to the specified minimum and maximum threshold value.

import pandas as pd
import numpy as np

x = pd.Series([-5, 5, 8, 15, -5])

print("The Series contains:")
print(x)

#clipping the series using
#minimum threshold value = 1
#maximum threshold value = 10
print("\nx.clip(1, 10) returns:")
print(x.clip(1, 10))

The output of the above code will be:

The Series contains:
0    -5
1     5
2     8
3    15
4    -5
dtype: int64

x.clip(1, 10) returns:
0     1
1     5
2     8
3    10
4     1
dtype: int64

Example: using clip() on selected series in a DataFrame

Similarly, the clip() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "x": [-10, -5, 5, 5, 8],
  "y": [-5, 5, 8, 15, -5],
  "z": [2, -5, -7, 25, 4]
})

print("The DataFrame is:")
print(df)

#clipping the 'y' Series
print("\ndf['y'].clip(1, 10) returns:")
print(df['y'].clip(1, 10))

The output of the above code will be:

The DataFrame is:
    x   y   z
0 -10  -5   2
1  -5   5  -5
2   5   8  -7
3   5  15  25
4   8  -5   4

df['y'].clip(1, 10) returns:
0     1
1     5
2     8
3    10
4     1
Name: y, dtype: int64

❮ Pandas Series - Functions