Skip to content
Advertisement

Formatting the print statement for multiple numpy arrays inside a dictionary Python

I am trying to modify my print statement within the for loop below so that it iterates through the list and the dictionary and prints the values of the first and second numpy arrays. In accordance to the Timeframes list. How can I modify the print statement below to get the Expected output below?

import numpy as np

Timeframes = ['Entirety:', 'Last Month:', 'Three Months:', 'Six Months:', 'Last Year:', 'Last Two Years:']
values = {[np.array([777.2062628 ,97.44704834 , 77.2062628 , 73.2062628 , 65.28 ,
       88.22628]), np.array([31040.02425794,   115.31287155,   115.31287155,   232.78473351,
         437.44961679,  4152.56739805])]}

for timeframe, values[0] in zip(Timeframes, iterator):
    print(f'{timeframe:<23} ${round(iterator[0][recordcounter],2):<15}${round(iterator[1][recordcounter],2):<14}')

Expected Output:

Entirety:               $777.2062628      $31040.02425794     
Last Month:             $97.44704834      $115.31287155          
Three Months:           $77.2062628       $115.31287155          
Six Months:             $73.2062628       $232.78473351         
Last Year:              $65.28            $437.44961679         
Last Two Years:         $88.22628         $4152.56739805 

Advertisement

Answer

If you make timeframes a numpy object with the same depth, you can stack the arrays together (or simply input them togeather).

In this case, let’s use vstack and transpose.

First we stack the arrays together:

import numpy as np

Timeframes = np.array([['Entirety:', 'Last Month:', 'Three Months:', 'Six Months:', 'Last Year:', 'Last Two Years:']])
values = np.array([[777.2062628 ,97.44704834 , 77.2062628 , 73.2062628 , 65.28 ,
       88.22628], [31040.02425794,   115.31287155,   115.31287155,   232.78473351,
         437.44961679,  4152.56739805]])

data=np.vstack((Timeframes,values))

data is now:

[['Entirety:' 'Last Month:' 'Three Months:' 'Six Months:' 'Last Year:'
  'Last Two Years:']
 ['777.2062628' '97.44704834' '77.2062628' '73.2062628' '65.28'
  '88.22628']
 ['31040.02425794' '115.31287155' '115.31287155' '232.78473351'
  '437.44961679' '4152.56739805']]

data.T is then:

[['Entirety:' '777.2062628' '31040.02425794']
 ['Last Month:' '97.44704834' '115.31287155']
 ['Three Months:' '77.2062628' '115.31287155']
 ['Six Months:' '73.2062628' '232.78473351']
 ['Last Year:' '65.28' '437.44961679']
 ['Last Two Years:' '88.22628' '4152.56739805']]

Finally we can do a simple loop on the transposed data:

for line in data.T:
    print(line[0],line[1],line[2])

This gives us:

Entirety: 777.2062628 31040.02425794
Last Month: 97.44704834 115.31287155
Three Months: 77.2062628 115.31287155
Six Months: 73.2062628 232.78473351
Last Year: 65.28 437.44961679
Last Two Years: 88.22628 4152.56739805

Note you can format out the output further by using a simple helper function:

def format_as_money(num):
    return '$'+str(round(float(num),2))

Then you can edit your print statement line:

for line in data.T:
    print(line[0],format_as_money(line[1]),format_as_money(line[2]))

Which gives:

Entirety: $777.21 $31040.02
Last Month: $97.45 $115.31
Three Months: $77.21 $115.31
Six Months: $73.21 $232.78
Last Year: $65.28 $437.45
Last Two Years: $88.23 $4152.57
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement