Maybe this has been answered before but I'm having a hard time searching the question. Assume I have the following data in a file:
date, id, int1, int2, int3
02/03/2015, 2, 23, 65, 99
10/06/2016, 4, 84, 12, 35
10/01/2017, 6, 53, 6, 78
I can quickly write a numpy snippet in the form:
import StringIO
import numpy as np
hdr = 'date, id, int1, int2, int3'
date = '''
02/03/2015, 2, 23, 65, 99
10/06/2016, 4, 84, 12, 35
10/01/2017, 6, 53, 6, 78
'''
lines = '%s%s' % (hdr, date)
pseudo_file = StringIO.StringIO(lines)
np_dtypes = 'S10,%s' % ','.join(['i4' for x in hdr.split(',')[1:]])
np1 = np.genfromtxt(pseudo_file, delimiter=',', names=True, dtype=np_dtypes)
print np1
print np1.dtype.names
print np1.shape
print np1['date']
print np1['int3']
This will give me the following output:
[('02/03/2015', 2, 23, 65, 99) ('10/06/2016', 4, 84, 12, 35)
('10/01/2017', 6, 53, 6, 78)]
('date', 'id', 'int1', 'int2', 'int3')
(3L,)
['02/03/2015' '10/06/2016' '10/01/2017']
[99 35 78]
One can see numpy was able to parse successfully the array. However, how do I split this in 2 portions:
- A 1D array with only the strings (the dates column);
- Another 1D array with only the integers.
The split should be done in a way that will keep the names structure of each columns.