8. Assignment Using Boolean Arrays Continued

Hello, currently I am working with Numpy exercises, here is the link:


I rather don’t have problem with Numpy logic, but in this exercise I don’t get the point how additional column is created.

zeros = np.zeros([taxi.shape[0], 1])
taxi_modified = np.concatenate([taxi, zeros], axis=1)

taxi.shape[0] gives us number of rows in table, what is the function of 1? Which way is created table only filled with “0” values?

taxi_modified - concatenates 2 tables by adding value ‘0’ to the each row but it doesn’t create header of new column , am I right?

1 Like

Hi @klausuts,

The np.zeros accepts an integer, or shape of the array (tuple or list) as input. If we provide it an integer, then it will generate a 1D array like this:

>>> np.zeros(5)
array([0., 0., 0., 0., 0.])

We cannot combine the 1D array to a 2D array (taxi). So here, we need to create a column of zeroes as 2D array. To create a 2D array, we have to provide 2 dimensions. That is the number of rows and the number of columns. For example:

>>> np.zeros([5, 2])
array([[0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])

In the case of np.zeros([taxi.shape[0], 1]), taxi.shape[0] will determine the number of rows, and 1 will be the number of columns.

I don’t understand this question. If you are asking, which way are we concatenating then, we are concatenating horizontally. axis=1 in np.concatenate([taxi, zeros], axis=1) represents horizontal axis.

You are right, it is not creating a header. However, both taxi and zeros don’t have headers. So there is no problem.

Hope this helps. Let me know if you have any further questions about it. I would be happy to help you. :slightly_smiling_face:



Hi, Sahil!

Yes, you’re right, I understood the sense of the function, thank you a lot.
Sorry for late response, too much work, complicated with free time now

1 Like