0

.I have a 2D array of strings and I intend to concatenate the strings along each row with a space between them. I used the following code, but it fails with a simple input, but it shouldn't be the case.

import numpy as np

a = np.array([['3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 
               '3','3', '3', '3', '3', '9', '11', '3', '3'],
              ['3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 
               '11', '9', '11', '3', '3', '3', '3', '3', '3']])

def concat(x, separator):
    return separator.join(x).strip()

b = np.apply_along_axis(concat, -1, a, ' ')

Expected:

b = ['3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 11 3 3',
     '3 3 3 3 3 3 3 3 3 3 3 11 9 11 3 3 3 3 3 3']

Output (See the end of the 2nd row of b):

b = ['3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 11 3 3',
     '3 3 3 3 3 3 3 3 3 3 3 11 9 11 3 3 3 3 3 ']

The last 3 is missing in the output. Any reason why this should happen?

Aman Dalmia
  • 356
  • 2
  • 10

1 Answers1

0

What does dtype=object mean while creating a numpy array? to understand why you need to add dtype=object to make it work

AdriBento
  • 589
  • 5
  • 16