Python | Pandas dataframe.clip_upper()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
dataframe.clip_upper() is used to trim values at specified input threshold. We use this function to trim all the values above the threshold of the input value to the specified input value.
Syntax: DataFrame.clip_upper(threshold, axis=None, inplace=False)
threshold : float or array_like
float: every value is compared to threshold.
array-like: The shape of threshold should match the object it’s compared to. When self is a Series, threshold should be the length. When self is a DataFrame, threshold should 2-D and the same shape as self for axis=None, or 1-D and the same length as the axis being compared.
axis : Align object with threshold along the given axis.
inplace : Whether to perform the operation in place on the data.
Returns: clipped : same type as input
Example #1: Use
clip_upper() function to trim values of a data frame above a given threshold value.
Now trim all the values above 8 to 8.
Example #2: Use
clip_upper() function to clips values in a dataframe with specific value for each cell of the dataframe.
For this purpose, we can use a numpy array, but the shape of array must be same as that of the dataframe.
Now apply these limits on the dataframe.
Each cell value has been trimmed based on the corresponding upper limit applied.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course