Programmatically add column names to numpy ndarray

后端 未结 2 1895
别跟我提以往
别跟我提以往 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])
    
    0 讨论(0)
  • 2021-02-05 15:43

    The problem is that you are thinking in terms of spreadsheet-like arrays, whereas NumPy does use different concepts.

    Here is what you must know about NumPy:

    1. NumPy arrays only contain elements of a single type.
    2. If you need spreadsheet-like "columns", this type must be some tuple-like type. Such arrays are called Structured Arrays, because their elements are structures (i.e. tuples).

    In your case, NumPy would thus take your 2-dimensional regular array and produce a one-dimensional array whose type is a 108-element tuple (the spreadsheet array that you are thinking of is 2-dimensional).

    These choices were probably made for efficiency reasons: all the elements of an array have the same type and therefore have the same size: they can be accessed, at a low-level, very simply and quickly.

    Now, as user545424 showed, there is a simple NumPy answer to what you want to do (genfromtxt() accepts a names argument with column names).

    If you want to convert your array from a regular NumPy ndarray to a structured array, you can do:

    data.view(dtype=[(n, 'float64') for n in csv_names]).reshape(len(data))
    

    (you were close: you used astype() instead of view()).

    You can also check the answers to quite a few Stackoverflow questions, including Converting a 2D numpy array to a structured array and how to convert regular numpy array to record array?.

    0 讨论(0)
提交回复
热议问题