Pandas – df.round() – How to round values in pandas

Spread the love

In this post, you will learn various ways to round data in pandas.

1 . Round to certain decimal places –

Read a dataset to work with.

import pandas as pd
import numpy as np

df = pd.read_csv('', sep='\t')

To round data in pandas, we can use the round method. The round method has a parameter decimal which takes the number of decimal places that you want to round.

Let’s say that you want to round the lifExp column to 2 decimal places.

df['lifeExp'] = df['lifeExp'].round(2)

You can also pass a dictionary to round several columns together. Let’s say you want to round lifeExp to 0 decimal places and gdpPercap to 3 decimal places.

df.round({'lifeExp':0, 'gdpPercap': 3})

You can also round the whole dataframe at once. Let’s say you want to round all the numeric column to 0 decimal places.


2 . Round up –

To round a numbers up, you can pass np.ceil to the apply method in pandas.


3 . Round Down –

To round down a number, you need to pass np.floor to the apply method in pandas.


Rating: 1 out of 5.

Leave a Reply