0

I am new to numpy library. and the problem I am facing is this. My initial array is this:

[[1.]
 [1.]
 [1.]
 [1.]]

and when I add a column using resize function :

arr.resize(4,2)

it changes to this :

[[1. 1.]
 [1. 1.]
 [0. 0.]
 [0. 0.]]

but what I want something like this:

[[1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]]

Is there a way to do this ? I tried finding the answer to this but couldn't. Thanks in Advance.

2
  • Read the resize docs carefully Commented Apr 5, 2020 at 14:59
  • @hpaulj I did and now I know why resize is working like this but I just want to add a new column or new row without changing the my previous array. How to do this using numpy ? Commented Apr 5, 2020 at 15:11

1 Answer 1

2

Got a solution for you, hope it helps!

len = np.ones((4,1))

array([[1.],
   [1.],
   [1.],
   [1.]])

len0 = np.zeros((4,1))

array([[0.],
   [0.],
   [0.],
   [0.]])

Using concatenate you can achieve your goal:

np.concatenate((len,len0),axis=1)

array([[1., 0.],
   [1., 0.],
   [1., 0.],
   [1., 0.]])

Happy learning!

Sign up to request clarification or add additional context in comments.

3 Comments

This solution helps and I also used np.row_stack and np.column_stack for adding rows and cols. that too solved my problem. Thanks
Was unaware of those functions! Thanks for the info
np.concatenate is the underlying function for all of the stack functions. Knowing how to use it is a good thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.