1

I am trying to convert numpy string array in to a float. Actually array has numbers as strings example ['22.96' '33.96'.....]

i am using astype to convert the string numpy array to float using the below code.

b = a.astype(np.float)

The above piece of code returning something like this: 1.04200000e+02

I tried np.float32 and also np.float64 but nothing working

Naresh
  • 33
  • 6

1 Answers1

0

Try this:

b=list(map(float,(list(map(str,a.astype(np.float))))))

Then it won't print it in scientific notation.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
  • This one also did not work out put is like this now 00000e+01 8.97000000e+00 4.10000000e+00\n 2.50000000e+00 2.50000000e+00 2.50000000e+00 2.06700000e+01\n 1.61800000e+01 2.50000000e+00 2.50000000e+00 7.76000000e+01\n 2.44300000e+01 3.41600000e+01 2.04900000e+01 1.08600000e+01\n 7.65000000e+00 2.50000000e+00 4.99000000e+00 2.50000000e+00\n 2.00400000e+01 2.5 – Naresh May 24 '17 at 02:25
  • No Luck , some new error TypeError: float() argument must be a string or a number, not 'map' – Naresh May 25 '17 at 02:05
  • Awesome , it worked !!!!!! Thank you. But why cannot we convert it to a float while reading from CSV it self using genfromtxt ? a = np.genfromtxt('Discover.csv', delimiter = ',',comments='^',dtype=str,usecols=3) is my code. Upvoted your answer but i dont have reputation for that vote to show up publicly – Naresh May 25 '17 at 04:22
  • genfromcsv does technically work, it returns a float but in scientific notation. To make it not in scientific notation, we have to convert it into a string and back into a float. – whackamadoodle3000 May 25 '17 at 04:28
  • You can accept my answer. That shows publicly. – whackamadoodle3000 May 25 '17 at 04:28
  • Done , figured out how to accept the answer LOL. – Naresh May 25 '17 at 04:35