NumPy structured arrays have named columns:

import numpy as np
    
a = range(100)
A = np.array(list(zip(*[iter(a)] * 2)), dtype=[('C1', 'int32'),('C2', 'int64')])
print(A.dtype)
[('C1', '<i4'), ('C2', '<i8')]

You can access the columns by name like this:

print(A['C1'])
# [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
#  50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98]

Note that using np.array with zip causes NumPy to build an array from a temporary list of tuples. Python lists of tuples use a lot more memory than equivalent NumPy arrays. So if your array is very large you may not want to use zip.

Instead, given a NumPy array A, you could use ravel() to make A a 1D array, and then use view to turn it into a structured array, and then use astype to convert the columns to the desired type:

a = range(100)
A = np.array(a).reshape( len(a)//2, 2)
A = A.ravel().view([('col1','i8'),('col2','i8'),]).astype([('col1','i4'),('col2','i8'),])
print(A[:5])
# array([(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)], 
#       dtype=[('col1', '<i4'), ('col2', '<i8')])

print(A.dtype)
# dtype([('col1', '<i4'), ('col2', '<i8')])
Answer from unutbu on Stack Overflow
🌐
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]: ...
Discussions

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
python - How do I add name columns to an existing numpy array? - Stack Overflow
I am trying to add column names to an existing numpy array. I have seen in this question that .dtype.names provides (and sets) the column names of a numpy array. However when I have an existing arr... More on stackoverflow.com
🌐 stackoverflow.com
October 24, 2020
python - How to add column names to Numpy array - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I am trying to add column names to a Numpy array, basically turning it into structured array even though the data types are all the same. More on stackoverflow.com
🌐 stackoverflow.com
arrays - Get the column names of a python numpy ndarray - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bobby Hadz
bobbyhadz.com › blog › numpy-ndarray-get-column-names
Get the column names of a NumPy ndarray in Python | bobbyhadz
We defined a plain NumPy array and used the unstructured_to_structured() method to convert the unstructured array to a structured array. We set the array's column names and used dtype.names to get a tuple containing the names.
🌐
IncludeHelp
includehelp.com › python › get-the-column-names-of-a-numpy-ndarray.aspx
Python - Get the column names of a NumPy ndarray
Suppose that we are given a .txt file that we want to load and generate a numpy array from it. This txt file contains a dataset in the form of rows and columns and each column has its name. When we print our created array, it will not show the column names instead, we need to index the array with the column name.
🌐
PyPI
pypi.org › project › named-array
named-array · PyPI
January 6, 2022 - This is a python module that extends numpy array to be able to have column names to index with
      » pip install named-array
    
Published: Jan 07, 2022
Version: 0.11
🌐
IncludeHelp
includehelp.com › python › add-rows-and-columns-headers-in-numpy-array.aspx
Add Rows and Columns Headers in NumPy Array
April 19, 2023 - result = pd.DataFrame(arr, index=name,columns=name) # Import numpy import numpy as np # Import pandas import pandas as pd # Creating an array arr = np.random.randint(0, 10, size=36).reshape(6, 6) # Display original array print("Original array:\n", arr, "\n") # index and column names row_names = ["Row_1", "Row_2", "Row_3", "Row_4", "Row_5", "Row_6"] column_names = ["A", "B", "C", "D", "E", "F"] # Giving names to rows and columns res = pd.DataFrame(arr, index=row_names, columns=column_names) # Display result pd.set_option("max_columns", 6) print("Result:\n", res)
🌐
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
🌐
NumPy
numpy.org › doc › stable › user › basics.rec.html
Structured arrays — NumPy v2.5 Manual
For instance, the C-struct-like memory layout of structured arrays in numpy can lead to poor cache behavior in comparison. A structured datatype can be thought of as a sequence of bytes of a certain length (the structure’s itemsize) which is interpreted as a collection of fields. Each field has a name, a datatype, and a byte offset within the structure.
🌐
w3resource
w3resource.com › python-exercises › pandas_numpy › pandas_numpy-exercise-2.php
Create Pandas DataFrame from NumPy array with custom columns
December 21, 2024 - Creating a NumPy Array: numpy_array ... "numpy_array". Defining Custom Column Names: column_names = ['Column1', 'Column2', 'Column3'] Defines custom column names in a list named "column_names"....
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)
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-numpy-how-to-get-colum-names-of-ndarray
Python NumPy: How to Get Column Names from a Structured Array (and Plain Arrays) | Tutorial Reference
The methods for getting "column names" primarily apply to structured arrays. When you load data into a NumPy array in a way that creates a structured array (e.g., using np.genfromtxt with names=True or by defining a structured dtype), the names of these fields (columns) are accessible via the dtype.names attribute.
🌐
GitHub
github.com › jax-ml › jax › issues › 2881
np.array named columns and different column types not working · Issue #2881 · jax-ml/jax
April 29, 2020 - Classical numpy arrays allow to specify column names/identifiers and assign different types to columns. From the documentation: Data-type consisting of more than one element: >>> x = np.array([(1,2...
Author: jax-ml
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.recarray.html
numpy.recarray — NumPy v2.5.dev0 Manual
The name of each column, e.g. ('x', 'y', 'z'). ... By default, a new array is created of the given shape and data-type. If buf is specified and is an object exposing the buffer interface, the array will use the memory from the existing buffer. In this case, the offset and strides keywords are available. ... View of the transposed array. ... Base object if memory is from some other object. ... An object to simplify the interaction of the array with the ctypes module.
🌐
GitHub
github.com › astropy › astropy › issues › 7320
initialize Table with numpy array and column names · Issue #7320 · astropy/astropy
March 21, 2018 - astropy v3.0.1; numpy v1.14.2 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-47-5d2d540b4498> in <module>() 3 names = ['col{0}'.format(i) for i in range(len(data))] ----> 4 Table(data, names=names) ~/anaconda3/lib/python3.6/site-packages/astropy/table/table.py in __init__(self, data, masked, names, dtype, meta, copy, rows, copy_indices, **kwargs) 403 names = [fix_column_name(name) for name in names] 404 --> 405 self._check_names_dtype(names, dtype, n_cols) 406 407 # Finally do the real initialization ~/ana
Author: astropy
🌐
GeeksforGeeks
geeksforgeeks.org › 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
August 21, 2020 - 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 › 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')
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])