Signup/Sign In
LAST UPDATED: MARCH 16, 2021

Pandas DataFrame clip() Method

In this tutorial, we will learn the Python pandas DataFrame.clip() method. This method trim values at input threshold(s). It assigns values outside the 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.

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

Syntax

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

Parameters

lower: It represents the float or array_like, default 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, default None. It represents the maximum threshold value. All values above this threshold will be set to it.

axis: It represents the int or str axis name, optional. It aligns the object with lower and upper along the given axis.

in place: 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 1: DataFrame.clip() method with a upper threshold

The below example shows how the DataFrame.clip() method trim values at the upper threshold.

#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(upper=6))

Once we run the program we will get the following result.


------DataFrame--------
col_1 col_2
0 9 -2
1 -3 -7
2 0 -6
3 -1 8
4 12 -5
------After clipping the DataFrame--------
col_1 col_2
0 6 -2
1 -3 -7
2 0 -6
3 -1 6
4 6 -5

Example 2: DataFrame.clip() method with a lower threshold

The below example shows how the DataFrame.clip() method trim values at the lower threshold.

#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(lower=-1))

Once we run the program we will get the following result.


------DataFrame--------
col_1 col_2
0 9 -2
1 -3 -7
2 0 -6
3 -1 8
4 12 -5
------After clipping the DataFrame--------
col_1 col_2
0 9 -1
1 -1 -1
2 0 -1
3 -1 8
4 12 -1

Example 3: The DataFrame.clip() Method with a lower and upper threshold

The below example shows how the DataFrame.clip() method trim values at the lower and upper threshold.

#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(-1,6))

Once we run the program we will get the following result.


------DataFrame--------
col_1 col_2
0 9 -2
1 -3 -7
2 0 -6
3 -1 8
4 12 -5
------After clipping the DataFrame--------
col_1 col_2
0 6 -1
1 -1 -1
2 0 -1
3 -1 6
4 6 -1

Example 4: The DataFrame.clip() method with a lower, upper threshold and inplace=True

The below example shows how the DataFrame.clip() method trim values at the lower and upper threshold when inplace=True .

The DataFrame.clip() method trims the values element-wise and returns None if inplace=True.

#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(-1,6,inplace=True))
print(df)

Once we run the program we will get the following result.


------DataFrame--------
col_1 col_2
0 9 -2
1 -3 -7
2 0 -6
3 -1 8
4 12 -5
------After clipping the DataFrame--------
None
col_1 col_2
0 6 -1
1 -1 -1
2 0 -1
3 -1 6
4 6 -1

Conclusion

In this tutorial, we learned the DataFrame.clip() method. We learned the syntax, parameters and by solving different examples we understood the DataFrame.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.