Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install numpy
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import numpy as np
DataFrame round()
The round()
method rounds the DataFrame output to a specified number of decimal places.
The syntax for this method is as follows:
DataFrame.round(decimals=0, *args, **kwargs)
Parameter | Description |
---|---|
decimals | Determines the specified number of decimal places to round the value(s). |
*args | Additional keywords are passed into a DataFrame/Series. |
**kwargs | Additional keywords are passed into a DataFrame/Series. |
For this example, the Bank of Canadaβs mortgage rates over three (3) months display and round to three (3) decimal places.
Code Example 1
df = pd.DataFrame([(2.3455, 1.7487, 2.198)], columns=['Month 1', 'Month 2', 'Month 3']) result = df.round(3) print(result)
- Line [1] creates a DataFrame complete with column names and saves it to
df
. - Line [2] rounds the mortgage rates to three (3) decimal places. This output saves to the
result
variable. - Line [3] outputs the result to the terminal.
Output
Month 1 | Month 2 | Month 3 | |
0 | 2.346 | 1.749 | 2.198 |
Another way to perform the same task is with a Lambda!
Code Example 2
df = pd.DataFrame([(2.3455, 1.7487, 2.198)], columns=['Month 1', 'Month 2', 'Month 3']) result = df.apply(lambda x: round(x, 3)) print(result)
- Line [1] creates a DataFrame complete with column names and saves it to
df
. - Line [2] rounds the mortgage rates to three (3) decimal places using a Lambda. This output saves to the
result
variable. - Line [3] outputs the result to the terminal.
π‘ Note: The output is identical to that of the above.
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.

At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Programming Instructor
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder