Programmatically add column names to numpy ndarray

后端 未结 2 1896
别跟我提以往
别跟我提以往 2021-02-05 14:45

I\'m trying to add column names to a numpy ndarray, then select columns by their names. But it doesn\'t work. I can\'t tell if the problem occurs when I add the names, or late

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 15:26

    Unfortunately, I don't know what is going on when you try to add the field names, but I do know that you can build the array you want directly from the file via

    data = np.genfromtxt(csv_file, delimiter=',', names=True)
    

    EDIT:

    It seems like adding field names only works when the input is a list of tuples:

    data = np.array(map(tuple,data), [(n, 'float64') for n in csv_names])
    

提交回复
热议问题