Python | Pandas dataframe.clip()
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() is used to trim values at specified input threshold. We can use this function to put a lower limit and upper limit on the values that any cell can have in the dataframe.
Syntax: DataFrame.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs)
lower : Minimum threshold value. All values below this threshold will be set to it.
upper : Maximum threshold value. All values above this threshold will be set to it.
axis : Align object with lower and upper along the given axis.
inplace : Whether to perform the operation in place on the data.
*args, **kwargs : Additional keywords have no effect but might be accepted for compatibility with numpy.
Example #1: Use
clip() function to trim values of a data frame below and above a given threshold value.
Now trim all the values below -4 to -4 and all the values above 9 to 9. Values in-between -4 and 9 remaining the same.
Notice, there is not any value in the data frame greater than 9 and smaller than -4
Example #2: Use
clip() function to clips using specific lower and upper thresholds per column element in the dataframe.
axis=0, then the value will be clipped across the rows. We are going to provide upper and lower threshold for all the column element (i.e. equivalent to the no. of rows)
Creating a Series to store the lower and upper threshold value for each column element.
Now we want to apply these limits on the dataframe.
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