Python | Pandas Series.round()

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 stroing in new colum
data['New']= data['New_Salary'].round()
  
# variable for max decimal places
dec_places = 2
  
# rounding of values and stroing in new colum
data['New2']= data['New_Salary'].round(dec_places)
  
# display
data.head(10)

chevron_right


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.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.