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.
When doing mathematical operations on series, many times the returned series is having decimal values and decimal values could go to upto many places. Pandas Series.round() method is used in such cases only to round of decimal values in series.
Syntax: Series.round(decimals=0, *args, **kwargs)
decimals: Int value, specifies upto what number of decimal places the value should be rounded of, default is 0.
Return type: Series with updated values
To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Since in the dataframe, there isn’t any series with decimal values more than 1 place. Hence the Salary column is divided by the Weight column first to get a series with decimal values. Since the returned series is having values with decimal upto 6 places. First a new series created by using round() method and another series new2 is created by passing a parameter of 2 to round() method to see working of this method. Before doing any operations, null rows were removed using dropna() method.
As shown in the Output image, the new series is completely rounded of with no decimal values and new2 series contains decimals upto 2 places after that only.
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