Good afternoon,
I would like to round columns in a dataframe to x number of places using the round half up function to ensure that any .5 values are always rounded up as per conventional rounding rules and to avoid the "bankers rounding" issue.
The dataframe sample I have is:
import pandas as pd
import decimal
Data = {'Code' : ['x', 'x', 'x'],
'Disaggregation' : ['a', 'b', 'Total'],
'Numerator' : [19.3276542, 82.136492834, 101.192747123]}
Data = pd.DataFrame(Data, columns = ['Code', 'Disaggregation', 'Numerator'])
The code I have got, which does not work is as follow:
Data['Numerator'] = (Decimal(Data['Numerator']).quantize(Decimal('.1'), rounding=ROUND_HALF_UP))
The following error is produced: conversion from Series to Decimal is not supported.
Dtypes of the dataframe is:
Code object
Disaggregation object
Numerator float64
dtype: object
Anyone have any clues how I can get this to work? (Of course the dataframe is much larger, thus I need to work on the column).
Thanks very much for this help on this in advance.
try:
change the number to your desired rounding value
output: