Python Forum
Panda Dataframe Rounding Issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Panda Dataframe Rounding Issue
#1
I am using Python 2.7 and I have data in a dataframe (df) and when I try rounding it does not consistently round, which appears to be due to the datatype being a float. My df has column value1 with values 0.121 to 0.130 I am testing on and I have used .round(2) and np.around(df['value'], decimals=2) but both round 0.125 to 0.12 and not 0.13. I attempted to use Decimal((df['value1'])).quantize(Decimal("0.01"), decimal.ROUND_HALF_UP).

However it caused an error to be raised "TypeError("Cannot convert %r to Decimal" % value)". I am also seeing TypeError : Cannot convert 0 0.121

Again, I feel I need to convert to decimal to correctly round, but nothing seems to work as expected for me. Any help is appreciated as this has frustrated me for the last couple days.

Thanks
Reply
#2
I got it to work right with df['value'].map(lambda x: round(x, 2)).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Nov-06-2018, 07:02 PM)ichabod801 Wrote: I got it to work right with df['value'].map(lambda x: round(x, 2)).

I did just try that and I do see it working, but I want to ask why I cannot use something built in to do what I wanted.

Would you be able to answer why Decimal() does not work with my Panda Dataframe. I thought the "best practice" would be to convert to decimal and then round, but Decimal() does not seem to work with dataframes, as it had an issue with 0.121.
Reply
#4
Well, map is built-in to the DataFrame, and lambda and round are built-in to Python, so that is something built-in.

As to why it won't accept Decimal, I'm not sure. TypeError makes me think it's conflicting with the type of the column. Can you post the full line of code and the full traceback?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Nov-06-2018, 07:22 PM)ichabod801 Wrote: Well, map is built-in to the DataFrame, and lambda and round are built-in to Python, so that is something built-in.

As to why it won't accept Decimal, I'm not sure. TypeError makes me think it's conflicting with the type of the column. Can you post the full line of code and the full traceback?

The code is Decimal((df['value1'])).quantize(Decimal("0.01"), decimal.ROUND_HALF_UP)
which produces the below
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-85-8612a11132b8> in <module>()
----> 1 Decimal((df['value1'])).quantize(Decimal("0.01"), decimal.ROUND_HALF_UP)

C:\Users\Owner\Anaconda2\lib\decimal.pyc in __new__(cls, value, context)
655 return self
656
--> 657 raise TypeError("Cannot convert %r to Decimal" % value)
658
659 # @classmethod, but @decorator is not valid Python 2.3 syntax, so

TypeError: Cannot convert 0 0.121
1 0.122
2 0.123
3 0.124
4 0.125
5 0.126
6 0.127
7 0.128
8 0.129
9 0.130
Name: value1, dtype: float64 to Decimal
Reply
#6
I see. You are sending the whole column to Decimal at once. It doesn't know how to handle that. It needs to convert the individual values one at a time.

This Stack Overflow has some ideas on using Decimal in a Series, but it doesn't look like an optimum idea.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding a new column to a Panda Data Frame rsherry8 2 2,083 Jun-06-2021, 06:49 PM
Last Post: jefsummers
  Issue with dataframe column nsadams87xx 0 1,946 May-29-2020, 02:00 AM
Last Post: nsadams87xx
  How to filter data using a panda.DateFrame.loc pawlo392 1 2,631 May-27-2019, 08:47 PM
Last Post: michalmonday
  do you know a code that will print all correlation values using numpty and panda? crispybluewaffle88 1 2,423 Mar-06-2019, 12:45 PM
Last Post: scidam
  Make panda dataframe output pretty carstenlp 2 2,907 Jan-17-2019, 10:04 AM
Last Post: carstenlp
  DataFrame index issue Astrikor 2 3,001 Aug-25-2018, 04:25 PM
Last Post: Astrikor
  rounding Stevger 0 1,905 Mar-13-2018, 09:32 PM
Last Post: Stevger
  Replacing values for specific columns in Panda data structure Padowan 1 14,636 Nov-27-2017, 08:21 PM
Last Post: Padowan
  Panda.read_cvs Issues Reading Certain Columns BlackHeart 5 6,090 Oct-27-2017, 04:29 PM
Last Post: Larz60+
  Panda Data Frame to Existing Multiple Sheets naveedraza 1 5,635 Jul-11-2017, 12:21 PM
Last Post: naveedraza

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020