Python 3 gets an error using numpy

Asked 2 years ago, Updated 2 years ago, 43 views

I am having trouble getting the following error using numpy in Python 3.

ValueError: could not convert string to float:b'0,000000'

The areaa.txt contains the following:

1000004,2193094,2193098,9886748,98867410,848450
2   4,219309    7,414822    7,414822    12,430150   12,430150   14,198310
3   8,000000    10,478795   10,478795   15,417747   15,417747   17,297929
1   11,000000   14,257995   14,257995   19,009302   19,009302   20,873072

The program is as follows:

import numpy as np    

cps, s_load, f_load, s_process, f_process, s_unload, f_unload=np.loadxt('arena.txt', unpack=True)

It doesn't matter if you don't use numpy.Thank you for your cooperation.

python python3 numpy

2022-09-29 22:06

3 Answers

Some countries use , (comma) as their decimal places (see Wikipedia).

read_csv in Pandas corresponds to the decimal point ,, and you can optionally set decimal=', ' to read the decimal number ,.In this case, if you use Pandas, you can write as follows.

import pandas as pd
df=pd.read_csv('arena.txt', header=None,delim_whitespace=True,decimal=',')


2022-09-29 22:06

ValueError: could not convert string to float:b'0,000000'

This says that the string type cannot be converted to float.
Apparently, when importing csv with numpy, it is retrieved by specifying float data by default.So,

np.loadtxt('area.txt', unpack=True)

np.genfromtxt("area.txt", dtype=None, delimiter="\t")

I think you can do it by changing it to .

https://review-of-my-life.blogspot.jp/2017/11/numpycsv.html


2022-09-29 22:06

I changed the decimal symbol of the number from comma , to period ., and it worked.

--user9156's comments and Questioner Comments.


2022-09-29 22:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.