0

I have a numpy array which looks like this

arr = np.array([['1','2','3','4','5','6']])

For the most common way to convert, loops are required like this.

for a in arr:
   for b in a:
      int(b)

However, I would like to convert all elements in the array without loops. How could I do that?

3
  • 1
    aren't they already strings? (numpy strings) Commented Apr 10, 2020 at 9:12
  • The question title says you want to convert to string, but your example shows that you're typecasting to int? Commented Apr 10, 2020 at 20:42
  • Ah...my bad. I was confused. It's to convert to integer. I'm sorry. Commented Apr 11, 2020 at 0:24

1 Answer 1

1

You can define the type of elements at the assignment:

arr = numpy.array(['1','2','3','4','5','6'], int)

or in case, you want to parse the type of every element in an initialized array, you can use astype() method

arr.astype('int')
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.