3

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]"

2
  • 4
    You have provided a well documented question. Is it possible to include your CSV file or an example of it also to reproduce this problem? Commented Oct 27, 2021 at 13:40
  • 1
    Numpy (nor python) can't convert a string like '8.25, 13.6, 18.8' to float. You need to split your string into smaller strings each representing a single number like ['8.25', '13.6', '18.8' ] Then each of these strings can be converted to floats. So after you've striped the brackets, add the line ` D = [ float(num) for num in D.split(', ') ] `. Commented Oct 27, 2021 at 13:43

1 Answer 1

1

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]')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.