numpy.loadtxt, ValueError: could not convert string to float

后端 未结 2 1901
自闭症患者
自闭症患者 2020-12-06 00:02

This is sample from large csv file:

6.1;6.1;7.2;8.9;5.0;
8.9;10.0;8.9;6.1;5.0;

If I try to read it to numpy array with np.loadtxt(\'t

相关标签:
2条回答
  • 2020-12-06 00:48

    So in my case the csv file had column names written in the first row. e.g.

    Column1,Column2,Column3
    5.4,2.3,2.4
    6.7,3.6,9.3
    

    So as given in the docs, all I had to do was use the skiprows parameter

    So the API call became,

    np.loadtxt('test.csv', delimiter=',', skiprows=1)

    0 讨论(0)
  • 2020-12-06 00:50

    You need to strip off the trailing ';' from the lines.

    A possible workaround if you know you have 5 columns is:

    np.loadtxt('test.csv', delimiter=';', usecols=range(5))
    

    Or, use genfromtext instead which handles missing values

    np.genfromtxt('test.csv', delimiter=';')[:,:-1]
    
    0 讨论(0)
提交回复
热议问题