1

I have a text file with 3240 rows and 1 column of values. Every 81 values represents one layer in a 2-D framework. So after 81 values, I would reach z=2, then 162, z=3 and so forth until then end (should be 40 x levels). I want to append the values in this text file to create a 2-D array, that basically represents a value at a specific (x,z) point. Here's what I have:

 df = pd.read_excel("/nfs/home11/grad/2017/et654149/ATM562/HW3/HW3atm562.xlsx")
 pivals=df.as_matrix()
 pi=[]
 l=np.arange(0, 3320, 41)
 count=0
 i=0
 for i in range(len(l)):
         pi[0][i].append(pivals[i])
         count=count+1

I keep getting the error "list index out of range" and honestly I have no idea if what I'm trying to accomplish is even correct with the code I am using. Eventually I want to plot this value on a contour filled plot.

Edit: I've tried using reshape before and after the comment below, but I am getting the error "index 40 is out of bounds for axis 1 with size 1".

1
  • This code works, but I after trying to simply plot this now on a contour plot, I am getting this error: ValueError: setting an array element with a sequence. Commented Oct 23, 2018 at 18:31

1 Answer 1

2

After investigate a multitude of errors here is a solution:

x = 40 
y=81 
z=[[0 for row in range(0,x)] for col in range(0,y)] 
for i in range(x): 
      for j in range(y): 
           z.append(pivals[i+j])
a = array(z)
a = a.reshape(81, 41)
Sign up to request clarification or add additional context in comments.

8 Comments

'numpy.ndarray' object is not callable is the error I get. Tried putting np.reshape(pivals [81, 40]) and get "index 40 is out of bounds for axis 1 with size 1".
All you did was run the code that I provided right? @EliTurasky
Yes, I simply ran what you provided.
Try converting the matrix to an array before by using np.asarray(pivals) before calling reshape
The as_matrix already makes pivals an array. That's why in my original comment I tried using brackets instead of parentheses (which I think you are supposed to since it is an array). However, doing this results in the error I posted above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.