How to convert a list of strings into a numeric numpy array?

Cristian J Estrada picture Cristian J Estrada · Mar 8, 2017 · Viewed 14.2k times · Source

I want to be able to calculate the mean, min and max of A:

 import numpy as np

 A = ['33.33', '33.33', '33.33', '33.37']

 NA = np.asarray(A)

 AVG = np.mean(NA, axis=0)

 print AVG

This does not work, unless converted to:

A = [33.33, 33.33, 33.33, 33.37]

Is it possible to perform this conversion automatically?

Answer

gzc picture gzc · Mar 8, 2017

you want astype

NA = NA.astype(float)