Pandas DataFrame - clip() function
The Pandas DataFrame 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
DataFrame.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 (Series or DataFrame or None) with the values outside the clip boundaries replaced or None if inplace=True.
Example: using threshold values as float
In the example below, a DataFrame df is created. The clip() function is used to clip the given dataframe according to the specified minimum and maximum threshold value.
import pandas as pd import numpy as np df = pd.DataFrame({ "x": [-10, -5, 5, 5, 8], "y": [-5, 5, 8, 15, -5] }) print("The DataFrame is:") print(df) #clipping the dataframe using #minimum threshold value = 1 #maximum threshold value = 10 print("\ndf.clip(1,10) returns:") print(df.clip(1,10))
The output of the above code will be:
The DataFrame is: x y 0 -10 -5 1 -5 5 2 5 8 3 5 15 4 8 -5 df.clip(1,10) returns: x y 0 1 1 1 1 5 2 5 8 3 5 10 4 8 1
Example: using threshold values as array
It is possible to specify clipping threshold as an array. In this case, the clipping is performed element-wise in the specified axis.
import pandas as pd import numpy as np df = pd.DataFrame({ "x": [-10, -5, 5, 5, 8], "y": [-5, 5, 8, 15, -5] }) #specifying minimum threshold value element-wise lbound = pd.Series([2, 3, 4, 5, 6]) #specifying maximum threshold value element-wise ubound = lbound + 5 print("The DataFrame is:") print(df, "\n") print("The minimum threshold value is:") print(lbound, "\n") print("The maximum threshold value is:") print(ubound, "\n") #clipping the dataframe column-wise print("df.clip(lbound, ubound, axis=0) returns:") print(df.clip(lbound, ubound, axis=0))
The output of the above code will be:
The DataFrame is: x y 0 -10 -5 1 -5 5 2 5 8 3 5 15 4 8 -5 The minimum threshold value is: 0 2 1 3 2 4 3 5 4 6 dtype: int64 The maximum threshold value is: 0 7 1 8 2 9 3 10 4 11 dtype: int64 df.clip(lbound, ubound, axis=0) returns: x y 0 2 2 1 3 5 2 5 8 3 5 10 4 8 6
❮ Pandas DataFrame - Functions