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

后端 未结 3 1171
情书的邮戳
情书的邮戳 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:49

    You can keep the column names if you use the names=True argument in the function np.genfromtxt

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

    Please note the dtype=float, that will convert your data to float. This is more efficient than using dtype=None, that asks np.genfromtxt to guess the datatype for you.

    The output will be a structured array, where you can access individual columns by their name. The names will be taken from your first row. Some modifications may occur, spaces in a column name will be changed to _ for example. The documentation should cover most questions you could have.

提交回复
热议问题