2

I am trying to subtract two columns in the dataframe but it is giving me same result for all the values?

Here is my data:

        a           b   
0   0.35805     -0.01315
1   0.35809     -0.01311
2   0.35820     -0.01300
3   0.35852     -0.01268

I tried following approach suggested in here, but it is repeating same result for me in all the rows.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Upriser
  • 418
  • 2
  • 7
  • 23
  • 1
    what is your expected output – BENY Mar 11 '19 at 00:58
  • @Wen-Ben it should be like 0.35805 - (-0.01315) = 0.3712, but the problem seems to be pandas is rounding my data. but actual data is 0.3580500019359589 - (-0.01315). How can i fix problem with rounding? – Upriser Mar 11 '19 at 01:03
  • How did you get your data into `pandas`? From a file or something else? @Upriser – Erfan Mar 11 '19 at 01:04
  • @Erfan from csv file. – Upriser Mar 11 '19 at 01:05
  • The reason you're getting the same result for all rows using the linked solution (`df.b - df.a`) is because the difference `b - a` *is* the same for all these rows. – Peter Leimbigler Mar 11 '19 at 01:05
  • @PeterLeimbigler but the solution should not be same. How can I fix that? – Upriser Mar 11 '19 at 01:06
  • 1
    @Upriser, if it's a problem that all column differences are identical, I guess the problem lies in how `a` and `b` are computed. As it stands, the correct subtraction result is a column of identical values, all `0.3712`. – Peter Leimbigler Mar 11 '19 at 01:08
  • You can try to change the `pandas settings`. Try to play with this: `pd.set_option('precision', 30)`. Change the value and find the sweet spot for your solution. – Erfan Mar 11 '19 at 01:08
  • @Erfan I tried changing the precision of pandas. It is giving me same result. Let me share data with you? – Upriser Mar 11 '19 at 01:10
  • Please do @Upriser – Erfan Mar 11 '19 at 01:11

2 Answers2

2

More like a precision issue , I always using decimal

from decimal import *
df.z.map(Decimal)-df.dist.map(Decimal)
Out[189]: 
0    0.3711999999999999796246319406
1    0.3712000000000000195232718880
2    0.3712000000000000177885484121
3    0.3712000000000000056454840802
dtype: object
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Actually weird that theres no solution for this within pandas. – Erfan Mar 11 '19 at 01:17
  • 1
    @Upriser, make sure that the incredibly high precision in your CSV is actually real, and not just a spurious result of floating-point arithmetic. For example, I don't think it makes sense to insist that `0.35805 - 0.01315 = 0.3711999999999999796246319406`. – Peter Leimbigler Mar 11 '19 at 02:27
0

I think this will work fine

df['a-b'] = df['a']-df['b']
Loochie
  • 2,414
  • 13
  • 20
  • I have tried this approach but it didn't work. The approach suggested by Wen-Ben in above solution is working for higher precision values. This approach will work for less precision and for integers, but for higher precision it failed for me. – Upriser Mar 12 '19 at 02:29