Methods to Round Values in Pandas DataFrame
Last Updated :
18 Aug, 2020
There are various ways to Round Values in Pandas DataFrame so let's see each one by one:
Let's create a Dataframe with 'Data Entry' Column only:
Code:
Python3
# import Dataframe class
# from pandas library
from pandas import DataFrame
# import numpy library
import numpy as np
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
6.89, 7.6454, 8.9659]}
# create a Dataframe
df = DataFrame(Myvalue,
columns = ['DATA ENTRY'])
# show the dataframe
df
Output:
Method 1: Using numpy.round().
Syntax: numpy.round_(arr, decimals = 0, out = None)
Return: An array with all array elements being
rounded off, having same type as input.
This method can be used to round value to specific decimal places for any particular column or can also be used to round the value of the entire data frame to the specific number of decimal places.
Example: Rounding off the value of the "DATA ENTRY" column up to 2 decimal places.
Python3
# import Dataframe class
# from pandas library
from pandas import DataFrame
# import numpy library
import numpy as np
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
6.89, 7.6454, 8.9659]}
# create a Dataframe
df = DataFrame(Myvalue,
columns = ['DATA ENTRY'])
# Rounding value of 'DATA ENTRY'
# column upto 2 decimal places
roundplaces = np.round(df['DATA ENTRY'],
decimals = 2)
# show the rounded value
roundplaces
Output:
Method 2: Using Dataframe.apply() and numpy.ceil() together.
Syntax: Dataframe/Series.apply(func, convert_dtype=True, args=())
Return: Pandas Series after applied function/operation.
Syntax: numpy.ceil(x[, out]) = ufunc ‘ceil’)
Return: An array with the ceil of each element of float data-type.
These methods are used to round values to ceiling value(smallest integer value greater than particular value).
Example: Rounding off the value of a particular column.
Python3
# import Dataframe from
# pandas library
from pandas import DataFrame
# import numpy
import numpy as np
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
6.89, 7.6454, 8.9659]}
# create a Dataframe
df = DataFrame(Myvalue,
columns = ['DATA ENTRY'])
# Here we are rounding the
# value to its ceiling values
roundUp = df['DATA ENTRY'].apply(np.ceil)
# show the rounded value
roundUp
Output:
Method 3: Using Dataframe.apply() and numpy.floor() together.
Syntax: numpy.floor(x[, out]) = ufunc ‘floor’)
Return: An array with the floor of each element.
These methods are used to round values to floor value(largest integer value smaller than particular value).
Example: Rounding off the value of the "DATA ENTRY" column to its corresponding Floor value.
Python3
# import Dataframe class
# from pandas library
from pandas import DataFrame
# import numpy library
import numpy as np
# dictionary
Myvalue = {'DATA ENTRY':[4.834327, 5.334477,
6.89, 7.6454, 8.9659] }
# create a Dataframe
df = DataFrame(Myvalue,
columns = ['DATA ENTRY'])
# Rounding of Value to its Floor value
rounddown = df['DATA ENTRY'].apply(np.floor)
# show the rounded value
rounddown
Output:
Similar Reads
Pandas DataFrame round() Method | Round Values to Decimal Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Pandas round() function rounds a DataFrame value to a number with given decimal places. This
2 min read
Pandas DataFrame quantile() Method | Find Quantile Values Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Pandas quantile() function returns values at the given quantile over the requested axis. Not
2 min read
Python | Pandas DataFrame.values Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
2 min read
Pandas DataFrame describe() Method The describe() method in Pandas generates descriptive statistics of DataFrame columns which provides key metrics like mean, standard deviation, percentiles and more. It works with numeric data by default but can also handle categorical data which offers insights like the most frequent value and the
4 min read
Pandas Series dt.round | Round Off DateTime Values to Given Frequency The Pandas dt.round() method rounds off the DateTime values in a series to a certain frequency level.Example:Python3 import pandas as pd sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
2 min read