Pandas Series round() Function:
Each value in a Series is rounded to the specified number of decimal places using the round() function of the Pandas Series.
Syntax:
Series.round(decimals=0)
Parameters
decimals: This is optional. It is an int that specifies the number of decimal places to round to. If decimals is negative, it indicates the number of positions to the left of the decimal point.
Return Value:
The rounded values of the given Series are returned by the round() function of the Pandas Series.
- Python Pandas Series abs() Function
- Python Pandas Series ge() Function
- Python Pandas Series cov() Function
Python Pandas Series round() Function
Example1
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series
- Apply round() function on the given series to round the elements present in the given series to 0 decimal places and print the result.
- Apply round() function on the given series by passing some random number as an argument to round the elements present in the given series upto the given decimal places and print the result.
- Here it rounds up to 1 decimal place.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([3.563, 4.324, 1.878, 5.352, 7.5902]) # Print the above given series print("The given series is:") print(gvn_series) print() # Apply round() function on the given series to round the elements # present in the given series to 0 decimal places and print the result. print("The elements of the given series after rounding to 0 decimal places:") print(gvn_series.round()) print() # Apply round() function on the given series by passing some random number as an # argument to round the elements present in the given series upto the given # decimal places and print the result. # Here it rounds upto 1 decimal place. print("The elements of the given series after rounding to 1 decimal places:") print(gvn_series.round(1))
Output:
The given series is: 0 3.5630 1 4.3240 2 1.8780 3 5.3520 4 7.5902 dtype: float64 The elements of the given series after rounding to 0 decimal places: 0 4.0 1 4.0 2 2.0 3 5.0 4 8.0 dtype: float64 The elements of the given series after rounding to 1 decimal places: 0 3.6 1 4.3 2 1.9 3 5.4 4 7.6 dtype: float64
Example2
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Apply round() function on the student_marks column of a dataframe by passing some random number as an argument to round the elements of the student_marks column upto the given decimal places and print the result.
- Here it rounds upto 2 decimal places.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary), index list as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "student_rollno": [1, 2, 3, 4], "student_marks": [80.890, 35.43, 25.113, 90.573]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply round() function on the student_marks column of a dataframe by passing # some random number as an argument to round the elements of the student_marks column # upto the given decimal places and print the result. # Here it rounds upto 2 decimal places. print("The elements of student_marks column of a dataframe after rounding to 2 decimals:") print(data_frme["student_marks"].round(2))
Output:
The given Dataframe: student_rollno student_marks virat 1 80.890 nick 2 35.430 jessy 3 25.113 sindhu 4 90.573 The elements of student_marks column of a dataframe after rounding to 2 decimals: virat 80.89 nick 35.43 jessy 25.11 sindhu 90.57 Name: student_marks, dtype: float64