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?
JavaScript
x
10
10
1
import numpy as np
2
3
Timeframes = ['Entirety:', 'Last Month:', 'Three Months:', 'Six Months:', 'Last Year:', 'Last Two Years:']
4
values = {[np.array([777.2062628 ,97.44704834 , 77.2062628 , 73.2062628 , 65.28 ,
5
88.22628]), np.array([31040.02425794, 115.31287155, 115.31287155, 232.78473351,
6
437.44961679, 4152.56739805])]}
7
8
for timeframe, values[0] in zip(Timeframes, iterator):
9
print(f'{timeframe:<23} ${round(iterator[0][recordcounter],2):<15}${round(iterator[1][recordcounter],2):<14}')
10
Expected Output:
JavaScript
1
7
1
Entirety: $777.2062628 $31040.02425794
2
Last Month: $97.44704834 $115.31287155
3
Three Months: $77.2062628 $115.31287155
4
Six Months: $73.2062628 $232.78473351
5
Last Year: $65.28 $437.44961679
6
Last Two Years: $88.22628 $4152.56739805
7
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:
JavaScript
1
9
1
import numpy as np
2
3
Timeframes = np.array([['Entirety:', 'Last Month:', 'Three Months:', 'Six Months:', 'Last Year:', 'Last Two Years:']])
4
values = np.array([[777.2062628 ,97.44704834 , 77.2062628 , 73.2062628 , 65.28 ,
5
88.22628], [31040.02425794, 115.31287155, 115.31287155, 232.78473351,
6
437.44961679, 4152.56739805]])
7
8
data=np.vstack((Timeframes,values))
9
data is now:
JavaScript
1
7
1
[['Entirety:' 'Last Month:' 'Three Months:' 'Six Months:' 'Last Year:'
2
'Last Two Years:']
3
['777.2062628' '97.44704834' '77.2062628' '73.2062628' '65.28'
4
'88.22628']
5
['31040.02425794' '115.31287155' '115.31287155' '232.78473351'
6
'437.44961679' '4152.56739805']]
7
data.T is then:
JavaScript
1
7
1
[['Entirety:' '777.2062628' '31040.02425794']
2
['Last Month:' '97.44704834' '115.31287155']
3
['Three Months:' '77.2062628' '115.31287155']
4
['Six Months:' '73.2062628' '232.78473351']
5
['Last Year:' '65.28' '437.44961679']
6
['Last Two Years:' '88.22628' '4152.56739805']]
7
Finally we can do a simple loop on the transposed data:
JavaScript
1
3
1
for line in data.T:
2
print(line[0],line[1],line[2])
3
This gives us:
JavaScript
1
7
1
Entirety: 777.2062628 31040.02425794
2
Last Month: 97.44704834 115.31287155
3
Three Months: 77.2062628 115.31287155
4
Six Months: 73.2062628 232.78473351
5
Last Year: 65.28 437.44961679
6
Last Two Years: 88.22628 4152.56739805
7
Note you can format out the output further by using a simple helper function:
JavaScript
1
3
1
def format_as_money(num):
2
return '$'+str(round(float(num),2))
3
Then you can edit your print statement line:
JavaScript
1
3
1
for line in data.T:
2
print(line[0],format_as_money(line[1]),format_as_money(line[2]))
3
Which gives:
JavaScript
1
7
1
Entirety: $777.21 $31040.02
2
Last Month: $97.45 $115.31
3
Three Months: $77.21 $115.31
4
Six Months: $73.21 $232.78
5
Last Year: $65.28 $437.45
6
Last Two Years: $88.23 $4152.57
7