Clip DataFrame
To clip a DataFrame in Pandas, meaning trimming the values at lower and upper thresholds, call clip() method on this DataFrame with required lower and upper threshold values.
In this tutorial, we will learn the syntax of clip() method, and how to use this method to clip the values of a given DataFrame, with examples.
clip() method assigns values outside boundary to boundary values. Thresholds can be singular values or array like. If an array is given for thresholds, the clipping is performed element-wise in the specified axis.
Syntax
The syntax of pandas DataFrame.clip() method is
DataFrame.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs)
where
Parameter | Value | Description |
---|---|---|
lower | float or array-like. default value is None. | Minimum threshold value. All values below this threshold will be set to it. A missing threshold (e.g NA) will not clip the value. |
upper | float or array-like. default value is None. | Maximum threshold value. All values above this threshold will be set to it. A missing threshold (e.g NA) will not clip the value. |
axis | int or str axis, name, optional | Align object with lower and upper along the given axis. |
inplace | bool default value is 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. |
Return Value
- DataFrame if inplace=True or
- None
Examples
Clip DataFrame with Lower and Upper Thresholds
In the following program, we take a DataFrame and clip it to a lower threshold of 0, and upper threshold of 5.
Example.py
import pandas as pd
data = {'col_0': [9, -3, 0, -1, 2], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)
result = df.clip(0, 5)
print(result)
Output
col_0 col_1
0 5 0
1 0 1
2 0 5
3 0 5
4 2 0
All the values that are less than 0 (lower threshold) are replaced with 0, and all the values greater than 5 (upper threshold) are replaced with 5, in the resulting DataFrame.
Inplace Clipping
In the following program, we pass True
for inplace
parameter. The original DataFrame must be modified with the clipping data.
Example.py
import pandas as pd
data = {'col_0': [9, -3, 0, -1, 2], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)
df.clip(0, 5, inplace=True)
print(df)
Output
col_0 col_1
0 5 0
1 0 1
2 0 5
3 0 5
4 2 0
Clip DataFrame with Axis and List for Threshold
In the following program, we clip the DataFrame with lists for thresholds and axis=’columns’. For axis=’columns’ we may provide axis=1.
Example.py
import pandas as pd
data = {'col_0': [9, -3, 0, -1, 2], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)
df.clip([0, 1], [5, 7], inplace=True, axis=1)
print(df)
Output
col_0 col_1
0 5 1
1 0 1
2 0 6
3 0 7
4 2 1
The first column is clipped with lower threshold of 0, and upper threshold of 5. The second column is clipped with lower threshold of 1 and upper threshold of 7.
Conclusion
In this Pandas Tutorial, we learned the syntax of DataFrame.clip() method and how to clip a DataFrame to lower and upper thresholds using pandas DataFrame.clip() method.