0

I'm working on banking project, where my team asks me to limit all float values to .2 precision.

My dataSet.head()

bank_stocks dataset

Goal: To find max of all stocks comparatively

My code & error i got

My present output:

Bank Ticker
BAC       54.900002
C            564.099976
GS         247.919998
JPM       70.080002
MS         89.300003
WFC      58.520000
dtype: float64

My expected output:

Bank Ticker
BAC       54.90
C            564.10
GS         247.91
JPM       70.08
MS         89.30
WFC      58.52
dtype: float64

Please help me with this!

Thirumalai vasan
  • 655
  • 2
  • 6
  • 19
  • 1
    by limiting do you want to round or floor them ? `df.select_dtypes('float').round(2)` the answers here mainly use print formatting which won't change the actual values of your data. – Umar.H May 03 '20 at 01:32

5 Answers5

1

You make a wrong use of "{:.2f}" in your print statement, you should use .format() to format your float.

You can use print("{:.2f}".format(some float)) to print a float with 2 decimals as explained here.

kiliz
  • 95
  • 4
  • this works with normal range, but it's iterating through dataset, which cause trouble yielding output! – Thirumalai vasan May 03 '20 at 00:18
  • 1
    I don't see why this shouldn't work : ``` for tick in tickers:   print(tick, "{:.2f}".format(bank_stocks[tick]['Close'].max()) ) ``` – kiliz May 03 '20 at 00:24
1

You could use pandas.Series.round method

I've got a toy DataFrame df:

l1        c1                           c2             
l2         a         b          c       a      b     c
0   0.066667  0.666667   6.666667  0.0002  0.002  0.02
1   0.133333  1.333333  13.333333  0.0004  0.004  0.04
2   0.200000  2.000000  20.000000  0.0006  0.006  0.06
3   0.266667  2.666667  26.666667  0.0008  0.008  0.08
df.xs('c', axis=1, level='l2').max().round(2)

Results into this:

l1
c1    26.67
c2     0.08
dtype: float64

I guess in your case

res = bank_stocks.xs('Close', axis=1, level='Stock Info').max().round(2)

would result into a Series res indexed by tickers with name Bank Ticker and desired values rounded up to 2 decimal places.

According to this answer you can then print it with

with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(res)
Hofsiedge
  • 127
  • 1
  • 8
  • am getting the following error on your code: 'float' object has no attribute 'round' – Thirumalai vasan May 03 '20 at 00:27
  • Could you provide your `bank_stocks` DataFrame structure? e.g., using `bank_stocks.head(5)`. I mean, it's fine to use for loops, but pandas provides you with ways to do it much more efficiently – Hofsiedge May 03 '20 at 00:29
  • am sorry, but we can't upload here on comments right? i'll show on main question – Thirumalai vasan May 03 '20 at 00:34
  • 1
    @Thirumalaivasan, I've edited my answer, check it out. This (or something close to this) might have better performance than Python for loops – Hofsiedge May 03 '20 at 01:41
  • bank_stocks.xs(key='Close',axis=1,level='Stock Info').max().round(2) i have used this code, which is really efficient, yes i agree yours its as great as it is. – Thirumalai vasan May 03 '20 at 01:56
  • 1
    @Thirumalaivasan, thank you, I've edited my answer again, liked your usage of `axis=1` instead of `.T` – Hofsiedge May 03 '20 at 01:58
0

I am not a very advanced python programmer but I this should work:

{:.2f}.format(max())

to say the least, that will print out 564.099976 as 564.09

Umar.H
  • 22,559
  • 7
  • 39
  • 74
0

This works for me, which is reliable, and without loop

bank_stocks.xs(key='Close',axis=1,level='Stock Info').max().round(2)

O(n)^2 is now O(n)

Thirumalai vasan
  • 655
  • 2
  • 6
  • 19
-1

This is not a clean solution but You can multiply the max stock price with 100, then do a floor division with 1 and then divide by 100.

This would solve your problems.

Anmol Batra
  • 159
  • 1
  • 8