pandas.DataFrame.clip#
- DataFrame.clip(lower=None,upper=None,*,axis=None,inplace=False,**kwargs)[source]#
Trim values at input threshold(s).
Assigns values outside boundary to boundary values. Thresholdscan be singular values or array like, and in the latter casethe clipping is performed element-wise in the specified axis.
- Parameters:
- lowerfloat or array-like, default None
Minimum threshold value. All values below thisthreshold will be set to it. A missingthreshold (e.gNA) will not clip the value.
- upperfloat or array-like, default None
Maximum threshold value. All values above thisthreshold will be set to it. A missingthreshold (e.gNA) will not clip the value.
- axis{{0 or ‘index’, 1 or ‘columns’, None}}, default None
Align object with lower and upper along the given axis.ForSeries this parameter is unused and defaults toNone.
- inplacebool, default False
Whether to perform the operation in place on the data.
- **kwargs
Additional keywords have no effect but might be acceptedfor compatibility with numpy.
- Returns:
- Series or DataFrame
Same type as calling object with the values outside theclip boundaries replaced.
See also
Series.clipTrim values at input threshold in series.
DataFrame.clipTrim values at input threshold in DataFrame.
numpy.clipClip (limit) the values in an array.
Examples
>>>data={"col_0":[9,-3,0,-1,5],"col_1":[-2,-7,6,8,-5]}>>>df=pd.DataFrame(data)>>>df col_0 col_10 9 -21 -3 -72 0 63 -1 84 5 -5
Clips per column using lower and upper thresholds:
>>>df.clip(-4,6) col_0 col_10 6 -21 -3 -42 0 63 -1 64 5 -4
Clips using specific lower and upper thresholds per column:
>>>df.clip([-2,-1],[4,5]) col_0 col_10 4 -11 -2 -12 0 53 -1 54 4 -1
Clips using specific lower and upper thresholds per column element:
>>>t=pd.Series([2,-4,-1,6,3])>>>t0 21 -42 -13 64 3dtype: int64
>>>df.clip(t,t+4,axis=0) col_0 col_10 6 21 -3 -42 0 33 6 84 5 3
Clips using specific lower threshold per column element, with missing values:
>>>t=pd.Series([2,-4,np.nan,6,3])>>>t0 2.01 -4.02 NaN3 6.04 3.0dtype: float64
>>>df.clip(t,axis=0)col_0 col_10 9.0 2.01 -3.0 -4.02 0.0 6.03 6.0 8.04 5.0 3.0