Signup/Sign In

Python Pandas Series.clip() Method

In this tutorial, we will learn the python pandas Series.clip() method. Using this method we can limit or trim the values present in Series by specifying the input thresholds which can be singular values or array. It returns Series by replacing values that are outside the clip boundaries and None if inplace=True.

The below shows the syntax of the Series.clip() method.

Syntax

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

Parameters

lower: It represents the float or array_like, the default value is None. It represents the minimum threshold value and all values below this threshold will be set to it.

upper: It represents the float or array_like, the default value is None. It represents the maximum threshold value. All values above this threshold will be set to it.

inplace: It represents the bool(True or False), and the default value is False. Whether to perform the operation in place on the data.

*args, **kwargs: It is the additional keywords that have no effect but might be accepted for compatibility with NumPy.

Example: Series.clip() method with a upper threshold

Here, in this example, we are trimming the Series values using the Series.clip() method with the upper threshold. We set the upper threshold value to 4 and the Series.clip() method trims the values which are above the threshold value and set them to the threshold value. See the below example.

#importing pandas library
import pandas as pd
series = pd.Series([8,3,-6,4.5])
print("------Series--------")
print(series)
print("------After clipping the Series--------")
print(series.clip(upper=4))


------Series--------
0 8.0
1 3.0
2 -6.0
3 4.5
dtype: float64
------After clipping the Series--------
0 4.0
1 3.0
2 -6.0
3 4.0
dtype: float64

Example: Series.clip() method with a lower threshold

Here, in this example, we are trimming the Series values using the Series.clip() method with the lower threshold. We set the lower threshold value to 4 and the Series.clip() method trims the values which are below the threshold value and set them to the threshold value. See the below example.

#importing pandas library
import pandas as pd
series = pd.Series([5,2,-6,3])
print("------Series--------")
print(series)
print("------After clipping the Series--------")
print(series.clip(lower=4))


------Series--------
0 5
1 2
2 -6
3 3
dtype: int64
------After clipping the Series--------
0 5
1 4
2 4
3 4
dtype: int64

Example: Series.clip() method with a upper and lower threshold

We can specify both the lower and upper threshold in the Series.clip() method and this method trims the Series values according to the specified lower and upper threshold values. See the below example.

#importing pandas library
import pandas as pd
series = pd.Series([8,-6,6,-1])
print("------Series--------")
print(series)
print("------After clipping the Series--------")
print(series.clip(-1,5))


------Series--------
0 8
1 -6
2 6
3 -1
dtype: int64
------After clipping the Series--------
0 5
1 -1
2 5
3 -1
dtype: int64

Example: Set inplace=True in Series.clip() method

Here, in this example, we set inpace=True in the Series.clip() method. The Series.clip() method trims values but it will not return a new object as the parameter inplace is set to True instead it returns None. See the below example.

#importing pandas library
import pandas as pd
series = pd.Series([8,-6,6,-1])
print("------Series--------")
print(series)
print("------After clipping the Series--------")
print(series.clip(-1,5,inplace=True))


------Series--------
0 8
1 -6
2 6
3 -1
dtype: int64
------After clipping the Series--------
None

Conclusion

In this tutorial, we learned the Series.clip() method. We learned the syntax, parameters and by solving different examples we understood the Series.clip() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.