Read CSV file to numpy array, first row as strings, rest as float

后端 未结 3 1177
情书的邮戳
情书的邮戳 2020-12-14 06:57

I have data stored in a CSV where the first row is strings (column names) and the remaining rows are numbers. How do I store this to a numpy array? All I can find is how t

3条回答
  •  半阙折子戏
    2020-12-14 07:37

    I'm not sure what you mean when you say you need the headers in the final version, but you can generate a structured array where the columns are accessed by strings like this:

    data = np.genfromtxt(path_to_csv, dtype=None, delimiter=',', names=True)
    

    and then access columns with data['col1_name'], data['col2_name'], etc.

提交回复
热议问题