I'm trying to write a 3 column data array to a text file. Each row has an associated date and time contained in another array as a string.
For example:
>>> data
array([[0, 1, 9],
[1, 1, 2],
[2, 4, 2],
[7, 3, 2],
[2, 2, 7],
[1, 0, 5]])
>>> dates
array([['2017-07-28T12:00:00'],
['2017-07-28T12:01:00'],
['2017-07-28T12:02:00'],
['2017-07-28T12:03:00'],
['2017-07-28T12:04:00'],
['2017-07-28T12:05:00']],
dtype='<U19')
I'm trying to use numpy.savetxt to write the strings and data to file. Based on the above data, the output file should look like this:
'2017-07-28T12:00:00', 0, 1, 9
'2017-07-28T12:01:00', 1, 1, 2
'2017-07-28T12:02:00', 2, 4, 2
'2017-07-28T12:03:00', 7, 3, 2
'2017-07-28T12:04:00', 2, 2, 7
'2017-07-28T12:05:00', 1, 0, 5
Unfortunately, I can't figure out how to correctly join the string and numeric data to work with savetxt. I have tried concatenating the arrays, joining them in tuples, stacking them, etc, but the solution has alluded me all day. Ideally, I'd like to stick with numpy and not break this out line-by-line in a loop or involve Pandas.
Can anyone recommend a way to make this work?
%sanswer, consider list approach. Iterate on the 'rows' and format and write one row at a time.