0

Having a little trouble on something that seems pretty simple. I want to join these two arrays to meet output:

array([['category_1', '4500', '5000'], ['category_2', '3200', '5000'], ['category_3', '3000', '5000'], ['category_4', '2000', '5000']], dtype='<U8')

I have some data

data = np.array([['category_1', '4500', '5000'], ['category_2', '3200', '5000']])

And I have this other data

other_data = np.array([['category_3', '3000', '5000'], ['category_4', '2000', '5000'])

And I get this error when I do this

np.concatenate(data, other_data)

TypeError: only integer scalar arrays can be converted to a scalar index
mike-gallego
  • 706
  • 2
  • 12
  • 27
  • 2
    Try `np.concatenate((data, other_data))`. `concatenate` takes a tuple input. – jpp Jun 25 '18 at 12:07
  • Oh wow that works! But why? A tuple is defined without parenthesis though. At least that's what I think. I can put a = 1, 2 and output will be a tuple – mike-gallego Jun 25 '18 at 12:08
  • 1
    Because that's the way NumPy has defined its function: check [the docs](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.concatenate.html). – jpp Jun 25 '18 at 12:09
  • In `np.concatenate(a, b)`, `a,b` is tuple, but it is unpacked into the arguments of the function. `a` corresponds to the `(a1,a2...)` argument of the function signature, ant `b` is taken to be the `axis` argument (that's why it complains about a `scalar index`). `concatenate` does not use a `*args` argument. – hpaulj Jun 25 '18 at 15:49

1 Answers1

1
data = np.array([['category_1', '4500', '5000'], ['category_2', '3200', '5000']])
other_data = np.array([['category_3', '3000', '5000'], ['category_4', '2000', '5000']])
np.concatenate((data, other_data), axis=0)
shahin mahmud
  • 945
  • 4
  • 11