• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Pandas Series round() Function

In Pandas, the round() function is used to round the elements of a Series to the specified number of decimal places. It is a convenient method to adjust the precision of numerical data in a Series.

In this article, I will explain the round() function, its syntax, parameters, and usage of how to get a new Pandas Series containing the rounded values based on the specified precision without modifying the original Series.

Key Points –

  • The round() function in Pandas Series adjusts the precision of numerical data within the series.
  • It allows rounding values to a specified number of decimal places or the nearest integer.
  • The decimals parameter specifies the number of decimal places to round to, with the default being 0 for rounding to the nearest integer.
  • Rounding performs on Series containing numeric data types, like integer and float.
  • It returns a new Pandas Series with rounded values the original Series remains unchanged.

Syntax of Pandas Series round() Function

Following is the syntax of the pandas series round() function.


# Syntax of Pandas Series round() 
Series.round(decimals=0, *args, **kwargs)

Parameters of the Series round() Function

Following are the parameters of the round() function.

  • decimals – This is an optional parameter specifying the number of decimal places to round the values to. If not provided, it defaults to 0, rounding to the nearest integer.
  • args, kwargs – Additional arguments and keyword arguments that can be passed to the underlying rounding function.

Return Value

It returns a new Series with the elements rounded to the specified number of decimal places.

Rounding a Series using round() Function

By default, the Pandas Series round() function rounds the elements of a Series to the nearest integer. If you want to round the Series elements with the specified decimal value, you can use a decimals parameter of the round() function.

First, let’s create a Pandas Series from a Python list.


import pandas as pd

# Creating a Series
data = [3.14159, 2.71828, 1.41421, 4.61803, 2.23607]
series = pd.Series(data)
print("Original Series:\n",series)

Yields below output.

pandas series round

Here, we used the round() method on the series object to round each value in the Series to specified decimal places. The result is assigned to a new Series named rounded_series.


# Rounding the Series to 3 decimal places
rounded_series = series.round(3)
print("Rounding three decimal places:\n",rounded_series)

In the above example, the original Series series is rounded to three decimal places using the round() function, and the result is stored in the variable rounded_series. This example yields the below output.

pandas series round

Rounding a Series to the Nearest Integer

To round a Pandas Series to the nearest integer, you can use the round() function without specifying any decimals. By default, if no decimals are provided, the function rounds to the nearest integer.


# Rounding the Series to the nearest integer
rounded_series = series.round()
print("Rounding nearest integer:\n",rounded_series)

# Output:
# Rounding nearest integer:
# 0    3.0
# 1    3.0
# 2    1.0
# 3    5.0
# 4    2.0
# dtype: float64

In the above example, the original Series series is rounded to the nearest integer using the round() function, and the result is stored in the variable rounded_series.

Rounding a Series to the Nearest 10

To round a Pandas Series to the nearest 10, use the round() function with a negative value for the decimals parameter, indicating the desired multiple to round to.


import pandas as pd

# Creating a Pandas Series
data = [32, 68, 94, 53, 88]
series = pd.Series(data)

# Rounding the Series to the nearest 10
rounded_series = series.round(-1)
print("Rounding nearest 10's:\n",rounded_series)

# Output:
# Rounding nearest 10's:
# 0    30
# 1    70
# 2    90
# 3    50
# 4    90
# dtype: int64

In the above example, the original Series series rounds to the nearest 10 using the round() function with decimals=-1, and the result is stored in the variable rounded_series.

Rounding Series to Specified Number of Significant Digits

Similarly, to round a Series to a specified number of significant digits, you can use the round() function with a negative value for the decimals parameter, indicating the number of significant digits.


import pandas as pd

# Creating a Pandas Series
data = [1234, 5678, 9101112]
series = pd.Series(data)

# Rounding the Series to 2 significant digits
rounded_series = series.round(-2)
print("Rounding significant digits:\n",rounded_series)

# Output:
# Rounding significant digits:
# 0       1200
# 1       5700
# 2    9101100
# dtype: int64

In this example, the original Series series rounds to 2 significant digits using the round() function with decimals=-2, and the result is stored in the variable rounded_series.

Rounding a Series to the Nearest 100

You can also round a Pandas Series to the nearest 100, to do so use the round() function with decimals=-2. This indicates rounding to the nearest multiple of 100.


import pandas as pd

# Creating a Pandas Series
data = [678, 3481, 8760, 1234, 5678]
series = pd.Series(data)

# Rounding the Series to the nearest 100
rounded_series = series.round(-2)
print("Rounding nearest:\n",rounded_series)

# Output:
# Rounding nearest:
# 0     700
# 1    3500
# 2    8800
# 3    1200
# 4    5700
# dtype: int64

In the above example, the original Series series rounds to the nearest 100 using the round() function with decimals=-2, and the result is stored in the variable rounded_series.

FAQ on Series round()

What does the Pandas Series round() function do?

The Pandas Series round() function performs the precision of numerical data within a Series. It allows you to round the values to a specified number of decimal places or the nearest integer, depending on the parameters you provide. This function is useful for formatting data for presentation or analysis purposes.

How can I round a Pandas Series to a specific number of decimal places?

You can round a Pandas Series to a specific number of decimal places using the round() function. By providing the desired number of decimal places as an argument to the function, you can round the values of the Series.

How can I round a Pandas Series to the nearest integer?

You can round a Pandas Series to the nearest integer by using the round() function without specifying any decimal places. By default, if you don’t provide any arguments, the function rounds to the nearest integer.

What data types are supported for rounding with the round() function?

The round() function in Pandas Series supports rounding for numerical data types such as float or integer. These are the most common data types encountered in numerical data analysis and manipulation. When you apply the round() function to a Pandas Series containing data of these types, it will round each value according to the specified precision or the nearest integer, depending on the parameters provided.

Is it possible to round a Pandas Series to the nearest multiple of 10?

It is possible to round a Pandas Series to the nearest multiple of 10 using the round() function. You can achieve this by specifying a negative value for the decimals parameter, indicating the desired multiple to round to.

Does the round() function modify the original Series?

The round() function in Pandas Series does not modify the original Series. Instead, it returns a new Series object containing the rounded values based on the specified precision or rounding rules. The original Series remains unchanged.

Conclusion

In this article, I have explained the Pandas Series round() function, which provides a straightforward and flexible way to adjust the precision of numerical data within a Series. and also explained how to round values to a specific number of decimal places, to the nearest integer, and to a certain multiple with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.