pandas.DataFrame.clip() Function | How to clip a dataframe

Spread the love

pandas.DataFrame.clip(): This function trim values at input threshold(s). 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, *args, **kwargs)[source]

Parameters:
lower : float or array_like, default None
Minimum threshold value. All values below this threshold will be set to it.

upper : float or array_like, default None
Maximum threshold value. All values above this threshold will be set to it.

axis : int or string axis name, optional
Align object with lower and upper along the given axis.

inplace : boolean, default False
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.

Returns:
Series or DataFrame
Same type as calling object with the values outside the clip boundaries replaced

#example program on pandas.DataFrame.clip() Function

import pandas as pd data = {'x': [1,2,3,4,5] 'y': [-9, -2, -6, -8, -5]} df = pd.DataFrame(data) print(df) print(df.clip(4, -4))

Output:

   x  y
0  1 -9
1  2 -2
2  3 -6
3  4 -8
4  5 -5
   x  y
0  1 -4
1  2 -2
2  3 -4
3  4 -4
4  4 -4

 

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *