Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string array to floats with python

I know there's a thousand other people who already asked this question but I've tried most of the solutions and nothing works. I'm using spyder and python 3.8.8.

I have a csv with some data I want to extract. When I import the data with pandas I want to convert it to a float but I'm currently unable. So far I have the following;

import pandas as pd
df = pd.read_csv("file.csv")
D = df.iloc[1,0] 
D = np.array(D)

gives me this array:

array('[8.25, 13.6, 18.8, 24.0, 29.02, 34.14, 44.63, 55.07, 65.31, 75.73, 85.96, 96.24, 107.0, 127.26, 136.68]dtype='<U103')

Firstly I tried

np.asarray(D, dtype=np.float64, order='C')

which gives me the following error:

AttributeError: 'numpy.ndarray' object has no attribute 'asarray'

then I tried

D = D.astype(np.float)

which gives the error

ValueError: could not convert string to float: '[8.25, 13.6, 18.8, 24.0, 29.02, 34.14, 44.63, 55.07, 65.31, 75.73, 85.96, 96.24, 107.0, 127.26, 136.68]'

So then I tried stripping some of the characters away like so

D = D.strip('[')
D = D.strip(']')
D = D.replace("'","")

then repeated my previous steps trying astype, and asarray with the newly stripped array as well as this one

D = np.asfarray(D,float)

but I again get errors such as

ValueError: could not convert string to float: '8.25, 13.6, 18.8, 24.0, 29.02, 34.14, 44.63, 55.07, 65.31, 75.73, 85.96, 96.24, 107.0, 127.26, 136.68'

Now I ran out of steam, can someone explain what's going wrong and how I can fix it please?

Thank you Aidan, the csv file is below, but you got me thinking, maybe I can make it in a better way. Right now I am appending the numbers to an array in a loop then writing them to a csv. Maybe someone can suggest a better way of formatting the array/csv to make this easier?

Pressures,M,D,Off "[5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130]" "[8.25, 13.6, 18.8, 24.0, 29.02, 34.14, 44.63, 55.07, 65.31, 75.73, 85.96, 96.24, 107.0, 127.26, 136.68]" "[0.53, 0.48, 0.43, 0.4, 0.37, 0.35, 0.33, 0.31, 0.3, 0.29, 0.28, 0.28, 0.26, 0.26, 0.26]" "[-3.11, -3.18, -3.38, -3.59, -3.73, -4.14, -4.69, -5.23, -5.77, -6.27, -6.66, -7.06, -7.02, -7.86, -8.21]"

like image 695
Tom Avatar asked Jul 19 '26 22:07

Tom


1 Answers

Your string looks like a python list, consider using eval() or pd.eval() to directly convert the string to a list before creating a dataframe.

pd.eval('[8.25, 13.6, 18.8, 24.0]')
like image 176
Lorenzo Donadio Avatar answered Jul 22 '26 11:07

Lorenzo Donadio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!