0

How would I be able to turn all float numpy arrays into strings arrays?

import numpy as np 

floats = np.array([1,3.4,0.678,11.1])

Expected output:

np.array(['1','3.4','0.678','11.1'])
1
  • 1
    floats = floats.astype(str) Commented Nov 17, 2021 at 8:50

2 Answers 2

2

The simplest way:

np.array([1,3.4,0.678,11.1]).astype(str)

output:

array(['1.0', '3.4', '0.678', '11.1'], dtype='<U32')
Sign up to request clarification or add additional context in comments.

Comments

0
strings = [str(value) for value in floats]

1 Comment

better to use floats = floats.astype(str). Otherwise list comprehension will have strings variable of type list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.