Skip to content
Related Articles

Related Articles

Improve Article
Python | Pandas Series.round()
  • Last Updated : 12 Jun, 2021

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) 
Parameters: 
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. 
 

Example: 
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.
 



Python3




# importing pandas module
import pandas as pd
 
# making data frame
   
# removing null values to avoid errors
data.dropna(inplace = True)
 
# creating new column with divided values
data["New_Salary"]= data["Salary"].div(data['Weight'])
 
# rounding of values and storing in new column
data['New']= data['New_Salary'].round()
 
# variable for max decimal places
dec_places = 2
 
# rounding of values and storing in new column
data['New2']= data['New_Salary'].round(dec_places)
 
# display
data.head(10)

Output: 
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

My Personal Notes arrow_drop_up
Recommended Articles
Page :