简体   繁体   中英

topography data, string '-' can't be converted to float

I'm attempting to import cornea topography data from a CSV file. The imshow fails to plot the data after slicing the axes and converting all to np.array, displaying the error message

"raise TypeError("Image data of dtype {} cannot be converted to " TypeError: Image data of dtype object cannot be converted to float"

If I don't convert the data to np.array and just write

topo1 = df.iloc[1:142, 1:142].astype(dtype=float)

the erroe message says:

"return arr.astype(dtype, copy=True) ValueError: could not convert string to float: '-'"

So it seems to me that there is a '-' character somewhere in my data that can't be converted to float but I've not been able to locate it and remove it.

Could someone please help me solve this problem?

All the best, Payman.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data= pd.read_csv (r'myfile.CSV', header=None)
df = pd.DataFrame(data=data)
# the first row of df is the x-axis range and the first column is the y-axis range
X= np.array(df.iloc[0, 1:142].astype(float))
Y= np.array(df.iloc[1:142, 0].astype(float))

topo1 = np.array(df.iloc[1:142, 1:142].astype(dtype=float, errors = 'ignore'))

plt.imshow(topo1)
plt.show()

When reading the.csv file, it appears that the key thing is to define the separator manually. As a result, the line should be:

df= read_csv (filename, header=None, error_bad_lines=False, sep=';')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM