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.
Pandas 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)
Parameters:
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.
# importing pandas as pd import pandas as pd # Creating a dataframe using dictionary df = pd.DataFrame({ "A" :[ - 5 , 8 , 12 , - 9 , 5 , 3 ], "B" :[ - 1 , - 4 , 6 , 4 , 11 , 3 ], "C" :[ 11 , 4 , - 8 , 7 , 3 , - 2 ]}) # Printing the data frame for visualization df |
Now trim all the values above 8 to 8.
# Clip all values below 2 df.clip_upper( 8 ) |
Output :
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.
# importing pandas as pd import pandas as pd # Creating a dataframe using dictionary df = pd.DataFrame({ "A" :[ - 5 , 8 , 12 , - 9 , 5 , 3 ], "B" :[ - 1 , - 4 , 6 , 4 , 11 , 3 ], "C" :[ 11 , 4 , - 8 , 7 , 3 , - 2 ]}) # upper limit for each individual column element. limit = np.array([[ 10 , 2 , 8 ], [ 3 , 5 , 3 ], [ 2 , 4 , 6 ], [ 11 , 2 , 3 ], [ 5 , 2 , 3 ], [ 4 , 5 , 3 ]]) # Print upper_limit limit |
Now apply these limits on the dataframe.
# applying different limit value # for each cell in the dataframe df.clip_upper(limit) |
Output :
Each cell value has been trimmed based on the corresponding upper limit applied.