Skip to content
Related Articles

Related Articles

Python | Ways to convert array of strings to array of floats
  • Last Updated : 26 Feb, 2019

Sometimes in a competitive coding environment, we get input in some other datatypes and we need to convert them in other forms this problem is same as that we have an input in the form of string and we need to convert it into floats.
Let’s discuss a few ways to convert an array of strings to array of floats.

Method #1 : Using astype




# Python code to demonstrate converting 
# array of strings to array of floats
# using astype
  
import numpy as np
  
# initialising array
ini_array = np.array(["1.1", "1.5", "2.7", "8.9"])
  
# printing initial array
print ("initial array", str(ini_array))
  
# conerting to array of floats
# using np.astype
res = ini_array.astype(np.float)
  
# printing final result
print ("final array", str(res))
Output:
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

 
Method #2: Using np.fromstring




# Python code to demonstrate converting 
# array of strings to array of floats
# using fromstring
  
import numpy as np
  
# initialising array
ini_array = np.array(["1.1", "1.5", "2.7", "8.9"])
  
# printing initial array
print ("initial array", str(ini_array))
  
# conerting to array of floats
# using np.fromstring
ini_array = ', '.join(ini_array)
ini_array = np.fromstring(ini_array, dtype = np.float
                                           sep =', ' )
  
# printing final result
print ("final array", str(ini_array))
Output:



initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

 
Method #3: Using np.asarray() and type




# Python code to demonstrate
# converting array of strings to array of floats
# using asarray
  
import numpy as np
  
# initialising array
ini_array = np.array(["1.1", "1.5", "2.7", "8.9"])
  
# printing initial array
print ("initial array", str(ini_array))
  
# conerting to array of floats
# using np.asarray
final_array = b = np.asarray(ini_array, 
        dtype = np.float64, order ='C')
  
# printing final result
print ("final array", str(final_array))
Output:
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :