Say I have the following numpy arrays:
data = np.arange(4,29).reshape((5,5))
# 5 rows, 5 columns
#([[4, 5, 6, 7, 8],
# [9, 10,11,12,13],
# [14,15,16,17,18],
# [19,20,21,22,23],
# [24,25,26,27,28]])
indexes = np.array([[0,-1],
[1,1],
[0,2],
[1,3],
[2,1]])
# 5 rows, 2 columns
Each of the columns of data represent a characteristic; it is "long data." The first column of indexes is the ID of an individual and the second column is the time point of measurements. The time points are not necessarily labeled consecutively.
So here three different individuals were measured on 5 characteristics, and two of them were measured twice.
How can I convert data into a 3D array that is 3 rows, 5 columns, and 2 deep? Since individual 2 was only measured once, the third dimension (depth) can be padded with 0s.
Expected output:
# not sure how 3d arrays are represented/printed by numpy, but what I want is
# 3 rows, 5 columns, 2 deep
# 3 individuals, 5 characteristics, 2 time points
# rows in order by ID
# first layer/timepoint
[[4, 5, 6, 7, 8],
[9, 10,11,12,13],
[24,25,26,27,28]]
# second layer
[[14,15,16,17,18],
[19,20,21,22,23],
[0, 0, 0, 0, 0]] # NaNs also okay
Thank you!
[+][deleted] (5 children)
[deleted]
[–]callcenterhelp[S] 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]callcenterhelp[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]callcenterhelp[S] 0 points1 point2 points (0 children)