Reputation: 185
I have a Numpy array with about 7 columns and I need to index certain values a lot but the current way I do this is not easily readable. for eg. I would like to say rates[-1][high]
or something similar. I thought that maybe I could just make variables for the eg. high = 2
but I use the same rates data in many different functions so I would have to set these variables in every single function or pass them as parameters but that's not very useful either. Is there a better way to do this? Thanks in advance!
if (
rates[-1][4] > rates[-1][1]
and rates[-2][4] > rates[-2][1]
and rates[-1][4] > rates[-2][4]
):
Upvotes: 0
Views: 112
Reputation: 3934
If I understand the question correctly you could set 'column' 4 to be it's own array.
import numpy as np
rates = rates = 1+.01*np.arange( 36 ).reshape( 4, 9 )
rates
# array([[1. , 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08],
# [1.09, 1.1 , 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17],
# [1.18, 1.19, 1.2 , 1.21, 1.22, 1.23, 1.24, 1.25, 1.26],
# [1.27, 1.28, 1.29, 1.3 , 1.31, 1.32, 1.33, 1.34, 1.35]])
high = rates[:, 4]
high
# array([1.04, 1.13, 1.22, 1.31])
Your formula becomes:
if (
high[-1] > rates[-1][1]
and high[-2] > rates[-2][1]
and high[-1] > rates[-2][4] # or > high[-2]
):
Upvotes: 1