Pandas DataFrame.clip: Clip Values in a DataFrame
Pandas DataFrame.clip
The DataFrame.clip method in pandas is used to clip values in a DataFrame to a specified range. Values below the lower bound are replaced with the lower bound, and values above the upper bound are replaced with the upper bound.
Syntax
The syntax for DataFrame.clip is:
DataFrame.clip(lower=None, upper=None, *, axis=None, inplace=False, **kwargs)Here, DataFrame refers to the pandas DataFrame on which the clipping operation is performed.
Parameters
| Parameter | Description |
|---|---|
lower | Specifies the lower bound. Values below this bound are replaced with the lower bound. Defaults to None. |
upper | Specifies the upper bound. Values above this bound are replaced with the upper bound. Defaults to None. |
axis | Specifies the axis along which the clipping is applied. Use 0 or 'index' for columns, and 1 or 'columns' for rows. Defaults to None (applies to all elements). |
inplace | If True, the operation is performed in place, modifying the original DataFrame. Defaults to False. |
**kwargs | Additional keyword arguments to pass to the method. |
Returns
A DataFrame with values clipped to the specified range. If inplace=True, returns None.
Examples
Clipping Values in a DataFrame
This example demonstrates how to use clip to clip values in a DataFrame to a specified range.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]})
# Clip values to a range between 2 and 40
result = df.clip(lower=2, upper=40)
print(result)Output
A B
0 2 10
1 2 20
2 3 30
3 4 40
4 5 40Clipping Values in a DataFrame with Missing Values
This example shows how clip handles missing values (NaN) when clipping values in a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame with missing values
df = pd.DataFrame({'A': [1, None, 3, 4, 5], 'B': [10, 20, None, 40, 50]})
# Clip values to a range between 2 and 40
result = df.clip(lower=2, upper=40)
print(result)Output
A B
0 2.0 10.0
1 NaN 20.0
2 3.0 NaN
3 4.0 40.0
4 5.0 40.0Clipping Values in a DataFrame In Place
This example demonstrates how to use clip with the inplace parameter to modify the original DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]})
# Clip values in place to a range between 2 and 40
df.clip(lower=2, upper=40, inplace=True)
print(df)Output
A B
0 2 10
1 2 20
2 3 30
3 4 40
4 5 40Clipping Values Along Rows of a DataFrame
This example shows how to use clip with the axis parameter to clip values along rows of a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Clip values along rows to a range between 2 and 8
result = df.clip(lower=2, upper=8, axis=1)
print(result)Output
A B C
0 2 4 7
1 2 5 8
2 3 6 8Summary
In this tutorial, we explored the DataFrame.clip method in pandas. Key takeaways include:
- Using
clipto limit values in a DataFrame to a specified range. - Handling missing values when clipping.
- Modifying the original DataFrame in place with the
inplaceparameter. - Applying clipping along rows or columns using the
axisparameter.