Import csv as float

Asked

Viewed 347 times

0

I am with the following problem, I am importing a basis for python, however I can not manipulate it (make average), by the same as str, I need to import it all values come as float.

data=pd.read_csv('C:\\caminho\\df.csv', index_col=0, dtype=np.float64)

I used the following syntax for import, he shows me an error in the last column, saying that can not convert, could help me?
I have already tried to transform the data frame after import by ulizing np.float64(data), but the same turns array, then all functions stop working.

3 answers

1

Good morning, Friend when analyzing your command it is correct, your problem may be in your file .csv, the pandas infer the dtype automatically based on imported content, although much of your csv’s content is numerical if there is a single line with string values, your dataframe will take over the dtype string. To resolve this problem just remove the corresponding line.

Hugs.

0

Assuming that these str can be converted into float, you can use pd.to_numeric():

df = pd.DataFrame([['1.', '2.'], ['3.', '4.']])
df = df.apply(pd.to_numeric)

0

You can try using the method pd.to_numeric() passing the parameter errors as follows:

df['suaColuna'] = pd.to_numeric(df['suaColuna'], errors='coerce')

// errors = 'coerce' -> se tiver um valor inválido ele vai virar NaN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.