Pandas Series clip() Function:
The clip() function of the Pandas Series trims values at the input thresholds. It assigns non-boundary values to boundary values. Thresholds can be single values or arrays, and in the latter case, clipping is done element-by-element along the given axis.
Syntax:
Series.clip(lower=None, upper=None, axis=None, inplace=False)
Parameters
lower: This is optional. It indicates the minimum threshold value as a float or array-like. All values that are less than(below) this threshold will be set to it. The value will not be clipped if a threshold (for example- NA) is missing. None is the default.
upper: This is optional. It indicates the maximum threshold value as a float or array-like. All values that are greater than(above) this threshold will be set to it. The value will not be clipped if a threshold (for example- NA) is missing. None is the default.
axis: This is optional. It is the int or str axis name to align the object with lower and upper along the specified axis. None is the default.
inplace: This is optional. If set to True, the operation is done in place on the data. False by default.
Return Value:
The same type as calling object with the values outside the clip boundaries replaced or None if inplace=True is returned.
- Python Pandas Series ge() Function
- Python Pandas DataFrame div() Function
- Python Pandas DataFrame mul() Function
Python Pandas Series clip() Function
Example1
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Apply clip() function on the given series to clip the values based on the given lower and upper threshold values and print the result.
- Here the lower threshold is 2 and the upper threshold = 12.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([-2, 4, -10, 5, 15, 20, 6, -3]) # Print the above given series print("The given series is:") print(gvn_series) print() # Apply clip() function on the given series to clip the values based on the # given lower and upper threshold values and print the result. # Here the lower threshold is 2 and upper threshold = 12 print("The given series after clipping:") print(gvn_series.clip(2, 12))
Output:
The given series is: 0 -2 1 4 2 -10 3 5 4 15 5 20 6 6 7 -3 dtype: int64 The given series after clipping: 0 2 1 4 2 2 3 5 4 12 5 12 6 6 7 2 dtype: int64
Example2
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Apply clip() function on the student_marks column of the dataframe to clip the values based on the given lower and upper threshold values and print the result.
- Here the lower threshold = 30 and upper threshold = 85.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "student_rollno": [1, 2, 3, 4], "student_marks": [80, 35, 25, 90]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply clip() function on the student_marks column of the dataframe # to clip the values based on the given lower and upper threshold values # and print the result. # Here the lower threshold = 30 and upper threshold = 85 print("The dataframe after clipping the student_marks column:") print(data_frme['student_marks'].clip(30, 85))
Output:
The given Dataframe: student_rollno student_marks virat 1 80 nick 2 35 jessy 3 25 sindhu 4 90 The dataframe after clipping the student_marks column: virat 80 nick 35 jessy 30 sindhu 85 Name: student_marks, dtype: int64