Pandas Series.clip()
Pandas Series.clip() Tutorial
In this tutorial, we'll explore the Series.clip() method in Pandas, which is used to limit the values in a Series to be within a specified range, with well detailed examples.
The syntax of Series.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs) is:
Series.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs)where
| Parameter | Description |
|---|---|
lower | Minimum threshold value. Values below this threshold will be set to this value. |
upper | Maximum threshold value. Values above this threshold will be set to this value. |
axis | The axis along which to clip the values. Default is None. |
inplace | Whether to perform the operation in-place or return a new Series. Default is False. |
The Series.clip() method modifies the values of the Series in-place unless inplace=True.
Examples for Series.clip() function
1. Clip values within a specified range
In this example, we'll use Series.clip() to limit the values in the Series int_series to be within a specified range of [15, 35].
Python Program
import pandas as pd
# Create a series
int_series = pd.Series([10, 20, 30, 40, 50])
print("Original Series:")
print(int_series)
# Clip values to be within the range [15, 35]
int_series.clip(lower=15, upper=35, inplace=True)
print("\nClipped Series:")
print(int_series)Output
Original Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Clipped Series:
0 15
1 20
2 30
3 35
4 35
dtype: int64The values lower than 15 have been adjusted to 15. The first element 10 has been clipped to 15.
And the values greater than 35 have been adjusted to 35. There are two elements 40 and 50 that have been clipped to 35.
Summary
In this tutorial, we've covered the Series.clip() method in Pandas, which is useful for limiting the values in a Series to be within a specified range.