Try:

myData.dtype.names

This will return a tuple of the field names.

In [10]: myData.dtype.names
Out[10]: ('TIME', 'FX', 'FY', 'FZ')
Answer from JoshAdel on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › get-the-column-names-of-a-numpy-ndarray.aspx
Python - Get the column names of a NumPy ndarray
When we print our created array, it will not show the column names instead, we need to index the array with the column name.
Discussions

python - numpy, named columns - Stack Overflow
Simple question about numpy: I load 100 values to a vector a. From this vector, I want to create an array A with 2 columns, where one column has name "C1" and second one "C2", one has type int32 and More on stackoverflow.com
🌐 stackoverflow.com
python - Programmatically add column names to numpy ndarray - Stack Overflow
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 later when I try to call th... More on stackoverflow.com
🌐 stackoverflow.com
python - Structured 2D Numpy Array: setting column and row names - Stack Overflow
I'm trying to find a nice way to take a 2d numpy array and attach column and row names as a structured array. For example: import numpy as np column_names = ['a', 'b', 'c'] row_names = ['1', '... More on stackoverflow.com
🌐 stackoverflow.com
python - How to add names to a numpy array without changing its dimension? - Stack Overflow
I have an existing two-column numpy array to which I need to add column names. Passing those in via dtype works in the toy example shown in Block 1 below. With my actual array, though, as shown in... More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.dtype.names.html
numpy.dtype.names — NumPy v2.2 Manual
>>> dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))]) >>> dt.names ('name', 'grades')
🌐
IncludeHelp
includehelp.com › python › add-rows-and-columns-headers-in-numpy-array.aspx
Add Rows and Columns Headers in NumPy Array
April 19, 2023 - Pass a list of row indexes to the index parameter and a list of column names to the columns parameter.
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-a-pandas-dataframe-from-a-numpy-array-and-specify-the-index-column-and-column-headers
Create a Pandas DataFrame from a Numpy array and specify the index column and column headers - GeeksforGeeks
July 15, 2025 - This method can be used if the index column and column header names follow some pattern. ... # Python program to Create a # Pandas DataFrame from a Numpy # array and specify the index column # and column headers # import required libraries import pandas as pd import numpy as np # creating a numpy array numpyArray = np.array([[15, 22, 43], [33, 24, 56]]) # defining index for the # Pandas dataframe index = ['Row_' + str(i) for i in range(1, len(numpyArray) + 1)] # defining column headers for the # Pandas dataframe columns = ['Column_' + str(i) for i in range(1, len(numpyArray[0]) + 1)] # generating the Pandas dataframe # from the Numpy array and specifying # details of index and column headers panda_df = pd.DataFrame(numpyArray , index = index, columns = columns) # printing the dataframe print(panda_df)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.dtype.names.html
numpy.dtype.names — NumPy v1.26 Manual
January 31, 2021 - >>> dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))]) >>> dt.names ('name', 'grades')
Find elsewhere
Top answer
1 of 2
15

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?.

2 of 2
3

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])
🌐
Readthedocs
scipy-cookbook.readthedocs.io › items › Recarray.html
Addressing Array Columns by Name — SciPy Cookbook documentation
There are two very closely related ways to access array columns by name: recarrays and structured arrays. Structured arrays are just ndarrays with a complicated data type: ... #!python numbers=disable In [1]: from numpy import * In [2]: ones(3, dtype=dtype([('foo', int), ('bar', float)])) Out[2]: array([(1, 1.0), (1, 1.0), (1, 1.0)], dtype=[('foo', '<i4'), ('bar', '<f8')]) In [3]: r = _ In [4]: r['foo'] Out[4]: array([1, 1, 1])
Top answer
1 of 4
1

The correct data input form for a structured array is a list of tuples:

In [71]: signal = [(1,2,3),(2,3,1),(3,2,1)] 
    ...: col_names = ('left','right','center') 
    ...: signal = np.array(signal, dtype = [(n, 'int16') for n in col_names])   
In [72]:                                                                        
In [72]: signal                                                                 
Out[72]: 
array([(1, 2, 3), (2, 3, 1), (3, 2, 1)],
      dtype=[('left', '<i2'), ('right', '<i2'), ('center', '<i2')])

1.16 has added a couple of functions that make it easier to convert to and from structured arrays:

In [73]: import numpy.lib.recfunctions as rfn                                   
In [74]: signal = np.array([[1,2,3],[1,2,3],[1,2,3]])                           
In [75]: dt = np.dtype([(n, 'int16') for n in col_names])                       
In [76]: dt                                                                     
Out[76]: dtype([('left', '<i2'), ('right', '<i2'), ('center', '<i2')])
In [77]: rfn.unstructured_to_structured(signal, dt)                             
Out[77]: 
array([(1, 2, 3), (1, 2, 3), (1, 2, 3)],
      dtype=[('left', '<i2'), ('right', '<i2'), ('center', '<i2')])

Applying this dt to signal has a problem:

In [82]: signal.view(dt)                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-f0fa01ce8128> in <module>
----> 1 signal.view(dt)

ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype

We can get around that by first converting signal to a compatible dtype:

In [83]: signal.astype('i2').view(dt)                                           
Out[83]: 
array([[(1, 2, 3)],
       [(1, 2, 3)],
       [(1, 2, 3)]],
      dtype=[('left', '<i2'), ('right', '<i2'), ('center', '<i2')])

But note that Out[83] shape is (3,1). The other arrays were shape (3,). view has always had this shape problem when converting to/from structured arrays. That's part of why the newer functions are easier to use.

2 of 4
1
values = [(1,2,3),(1,2,3),(1,2,3)]
signal = np.array(values, [('left', '<i2'), ('center', '<i2'), ('right', '<i2')])
signal['right']
array([3, 3, 3], dtype=int16)
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.recarray.html
numpy.recarray — NumPy v2.5.dev0 Manual
The desired data-type. By default, the data-type is determined from formats, names, titles, aligned and byteorder. ... A list containing the data-types for the different columns, e.g. ['i4', 'f8', 'i4']. formats does not support the new convention of using types directly, i.e.
🌐
GeeksforGeeks
geeksforgeeks.org › create-a-dataframe-from-a-numpy-array-and-specify-the-index-column-and-column-headers
Create a DataFrame from a Numpy array and specify the index column and column headers - GeeksforGeeks
July 28, 2020 - # importiong the modules import pandas as pd import numpy as np # creating the Numpy array array = np.array([['Aditya', 20], ['Samruddhi', 15], ['Rohan', 21], ['Anantha', 20], ['Abhinandan', 21]]) # creating a list of index names index_values = ['A', 'B', 'C', 'D', 'E'] # creating a list of column names column_values = ['Names', 'Age'] # creating the dataframe df = pd.DataFrame(data = array, index = index_values, columns = column_values) # displaying the dataframe print(df) Output : Example 3 : Python3 ·
🌐
Stack Overflow
stackoverflow.com › questions › 52149376 › how-to-get-column-names-from-my-numpy-array
python - How to get column names from my numpy array? - Stack Overflow
Copyimport re f = open('f.csv','r') alllines = f.readlines() columns = re.sub(' +',' ',alllines[0]) #delete extra space in one line columns = columns.strip().split(',') #split using space print(columns)
🌐
Data Carpentry
datacarpentry.github.io › semester-biology › materials › a-brief-introduction-to-numpy
A brief introduction to Numpy · Data Carpentry for Biologists
data = np.genfromtxt(‘C:pathtofiledatafile.csv’, names=[‘column1’, ‘column2’, ‘column3’], delimiter=’,’)
🌐
NumPy
numpy.org › doc › stable › user › basics.rec.html
Structured arrays — NumPy v2.5 Manual
A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes. In addition to field names, fields may also have an associated title, an alternate name, which is sometimes used as an additional description or alias for the field.
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.dtype.names.html
numpy.dtype.names — NumPy v1.25 Manual
>>> dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))]) >>> dt.names ('name', 'grades')