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 Overflowpython - numpy, named columns - Stack Overflow
python - Programmatically add column names to numpy ndarray - Stack Overflow
python - How to add names to a numpy array without changing its dimension? - Stack Overflow
python - How to add column names to Numpy array - Stack Overflow
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')])
I know this is an old question, but a more recently available option would be to try using pandas. The DataFrame type is designed for structured data like this, where columns are named and can be of different types.
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:
- NumPy arrays only contain elements of a single type.
- 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?.
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])
First because your question asks about giving names to arrays, I feel obligated to point out that using "structured arrays" for the purpose of giving names is probably not the best approach. We often like to give names to rows/columns when we're working with tables, if this is the case I suggest you try something like pandas which is awesome. If you simply want to organize some data in your code, a dictionary of arrays is often much better than a structured array, so for example you can do:
Y = {'ID':X[0], 'Ring':X[1]}
With that out of the way, if you want to use a structured array, here is the clearest way to do it in my opinion:
import numpy as np
RING = [1,2,2,3,3,3]
ID = [1,2,3,4,5,6]
X = np.array([ID, RING])
dt = {'names':['ID', 'Ring'], 'formats':[int, int]}
Y = np.zeros(len(RING), dtype=dt)
Y['ID'] = X[0]
Y['Ring'] = X[1]
store-different-datatypes-in-one-numpy-array another page including a nice solution of adding name to an array which can be used as column Example:
r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')
# x1, x2, x3 are flatten array
# a,b,c are field name
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.
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)
Use the following code:
import 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)
Assume CSV file is like this:
xy wz hi kq
0 10 5 6
1 2 4 7
2 5 2 6
Let's assume your csv file looks like
xy,wz,hi,kq
0,10,5,6
1,2,4,7
2,5,2,6
Then use pd.read_csv to dump the file into a dataframe
df = pd.read_csv('gbk_X_1.csv')
The dataframe now looks like
df
xy wz hi kq
0 0 10 5 6
1 1 2 4 7
2 2 5 2 6
It's three main components are the
data which you can access via the
valuesattributedf.values array([[ 0, 10, 5, 6], [ 1, 2, 4, 7], [ 2, 5, 2, 6]])index which you can access via the
indexattributedf.index RangeIndex(start=0, stop=3, step=1)columns which you can access via the
columnsattributedf.columns Index(['xy', 'wz', 'hi', 'kq'], dtype='object')
If you want the columns as a list, use the to_list method
df.columns.tolist()
['xy', 'wz', 'hi', 'kq']