1

I have known columns that I want to create as an array of shape 5x269.

metadata_dtype = np.dtype([('frameNumber', np.int), ('year', np.int), ('day', np.int), ('seconds', np.int), ('sosStepIndex', np.int)])
frame_metadata_data = np.array([(frames), (years), (days), (seconds), (sos_step_index)], metadata_dtype)

frames, years, days, seconds, and sos_step_index are all 1x269 arrays. Unfortunately, when I execute the code above, I get a 5x269 array, but each item in the array contains a 1x5 list of the same repeated value.

4
  • The list of tuples is supposed to be one tuple per record. Looks like you are trying (and failing) to provide one tuple per field.. Look again at the docs and examples. Commented Sep 24, 2020 at 0:17
  • Do you have recommended docs and examples? I'm posting on Stack Overflow for the first time because I haven't been able to find an example of what I'm trying to do. Commented Sep 24, 2020 at 0:20
  • How about the numpy structured array doc page? numpy.org/doc/stable/user/basics.rec.html Commented Sep 24, 2020 at 0:22
  • Those are all examples by row, not by column. How do I convert individual columns into combined rows? Commented Sep 24, 2020 at 0:26

1 Answer 1

1

You can create an empty array and update it:

metadata_dtype = np.dtype([('frameNumber', np.int), ('year', np.int), ('day', np.int), ('seconds', np.int), ('sosStepIndex', np.int)])
frame_metadata_data = np.empty(len(frames),dtype=metadata_dtype)
frame_metadata_data['frameNumber'] = frames
frame_metadata_data['year'] = years
frame_metadata_data['day'] = days
frame_metadata_data['seconds'] = seconds
frame_metadata_data['sosStepIndex'] = sos_step_index

UPDATE: In case you want a non-structured array:

frame_metadata_data = np.stack((frames, years, days, seconds, sos_step_index)).T

In this case, you will have to have a single datatype for all arrays and will not be able to call them by names, rather you can use indices to call them. For example, frames would be frame_metadata_data[:,0]

Another approach is using Pandas:

frame_metadata_data = pd.DataFrame({'frameNumber':frames, 'year':years, 'day':days, 'seconds':seconds, 'sosStepIndex':sos_step_index})
Sign up to request clarification or add additional context in comments.

2 Comments

This is great in that it gets me a lot closer to what I need. Unfortunately, it seems to create a 1x256 array where each entry is the list (frameNumber, year, day, seconds, sosStepIndex). I need a 5x256 array
@ElizabethM If you do not want a structured array (which is what this post provides), you will have to have the same datatype for all your arrays (non-structured numpy arrays have single dtype). And in that case, you will not be able to name columns. I will add that approach as well as a pandas approach that might be more suitable for your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.