w3resource

Pandas: Series - clip() function

Trim values at input in Pandas

The clip() function is used to 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:

Series.clip(self, lower=None, upper=None, axis=None, inplace=False, *args, **kwargs)
Pandas Series clip image

Parameters:

Name Description Type/Default Value Required / Optional
lower Minimum threshold value. All values below this threshold will be set to it. float or array_like
Default Value: None
Required
upper Maximum threshold value. All values above this threshold will be set to it. float or array_like
Default Value : None
Required
axis Align object with lower and upper along the given axis. int or str axis name Optional
inplace Whether to perform the operation in place on the data. bool
Default Value : False
Required
*args, **kwargs Additional keywords have no effect but might be accepted for compatibility with numpy. Required

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

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
data = {'c_0': [7, -3, 0, -4, 5], 'c_1': [-2, -5, 6, 9, -5]}
df = pd.DataFrame(data)
df

Output:

  c_0	c_1
0	 7	-2
1	-3	-5
2	 0	 6
3	-4	 9
4	 5	-5

Example - Clips per column using lower and upper thresholds:

Python-Pandas Code:

import numpy as np
import pandas as pd
data = {'c_0': [7, -3, 0, -4, 5], 'c_1': [-2, -5, 6, 9, -5]}
df = pd.DataFrame(data)
df.clip(-2, 6)

Output:

  c_0	c_1
0	 6	-2
1	-2	-2
2	 0	 6
3	-2	 6
4	 5	-2
Pandas Series clip image

Example - Clips using specific lower and upper thresholds per column element:

Python-Pandas Code:

import numpy as np
import pandas as pd
t = pd.Series([0, -4, -2, 5, 3])
t

Output:

0    0
1   -4
2   -2
3    5
4    3
dtype: int64

Python-Pandas Code:

import numpy as np
import pandas as pd
data = {'c_0': [7, -3, 0, -4, 5], 'c_1': [-2, -5, 6, 9, -5]}
df = pd.DataFrame(data)
t = pd.Series([0, -4, -2, 5, 3])
df.clip(t, t + 2, axis=0)

Output:

     c_0	c_1
0	   2	0
1	  -3   -4
2	   0	0
3	   5	7
4	   5	3

Previous: Boolean Series in Pandas
Next: Compute correlation



Follow us on Facebook and Twitter for latest update.