0

I have a 3D numpy array of the shape (1, 60, 1). Now I need to remove the first value of the second dimension and instead append a new value at the end.

If it was a list, the code would look somewhat like this:

x = [1, 2, 3, 4]
x = x[1:]
x.append(5)

resulting in this list: [2, 3, 4, 5]

What would be the easiest way to do this with numpy?

I have basically never really worked with numpy before, so that's probably a pretty trivial problem, but thanks for your help!

2
  • Can you paste sample code that generates your numpy array ? Commented Aug 23, 2020 at 10:05
  • Use np.append(x, 5) Commented Aug 23, 2020 at 10:08

1 Answer 1

3
import numpy as np

arr = np.arange(60)   #creating a nd array with 60 values  
arr = arr.reshape(1,60,1)   # shaping it as mentiond in question
arr = np.roll(arr, -1)   # use np.roll to circulate the array left or right (-1 is 1 step to the left)
#Now your last value is in the second last position, the second last value in the third last pos and so on (Your first value moves to the last position)  
arr[:,-1,:] = 1000  # index the last location and add the values you want  
print(arr)
Sign up to request clarification or add additional context in comments.

Comments

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.