Suppose I have a numpy
array that maps between IDs of two item types:
[[1, 12],
[1, 13],
[1, 14],
[2, 13],
[2, 14],
[3, 11]]
I would like to rearrange this array such that each row in the new array represents all items that matched the same ID in the original array. Here, each column would represent one of the mappings in the original array, up to a specified shape restriction on the number of columns in the new array. If we wanted to obtain this result from the above array, ensuring we only had 2 columns, we would obtain:
[[12, 13], #Represents 1 - 14 was not kept as only 2 columns are allowed
[13, 14], #Represents 2
[11, 0]] #Represents 3 - 0 was used as padding since 3 did not have 2 mappings
The naïve approach here would be to use a for-loop that populates the new array as it encounters rows in the original array. Is there a more efficient means of accomplishing this with numpy
's functionality?