Decimal class rounding in Pandas

允我心安 提交于 2019-12-24 15:33:28

问题


i have problems rounding Decimals() inside a Pandas Dataframe. The round() method does not work and using quantize() neither. I've searched for a solution with no luck so far.

round() does nothing, i asume it is meant for float numbers

quantize() won't work because it is not a DataFrame function

Any thoughts?


回答1:


Since pandas has no quantize method i used the following to solve the problem:

out.applymap(lambda x: x.quantize(dc.Decimal('1.00')))



回答2:


To round your decimal to 2 significant figures for example:

round(df['yourSeries'].astype('float'), 2)

Or if you just want an int:

round(df['yourSeries'].astype('float'))



来源:https://stackoverflow.com/questions/54773447/decimal-class-rounding-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!