1

i have an dataframe like this:

      quantity  price
202     1       3.99
287     1       3.99
309     1       3.99
1345    3       11.97
1681    1       14.36
... ... ...
275754  1       3.59
275922  1       3.99
275927  1       3.99
276012  1       3.99
276065  1       3.59

And i need unique itemprices:

df['item_price'] = (df['price'] / df['quantity'])

but

df.item_price.unique()

gives this: (note the 3.99)

[ 3.99,  0.  ,  3.99,  3.59, 14.36]

i have tried many solutions (strip(),str.replace(' ', ''), return_index=False, return_inverse=False, return_counts=False ... and so on), But without luck. 3.99 is always doubled. Whats wrong with that?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 5
    You should round the numbers before making them unique, because they probably have differences in some low-order fraction digits that aren't shown by default. Floating point has inherent inaccuracies like this. – Barmar Aug 31 '23 at 16:35
  • 2
    Try: `df['item_price'].round(2).unique()` – mozway Aug 31 '23 at 16:37
  • @mozway gives the simplest solution for my current problem. I hate floats. – Ingo Eyring Aug 31 '23 at 16:45
  • @IngoEyring you may have an easier time if you redefine the units to be integers, even if this means a little upfront pain thinking about, say transactions in micro-Euros – ti7 Aug 31 '23 at 17:37

0 Answers0