Select each row of data with different column indices #1906
-
Dear experts, I have a basic question, but I couldn't find the solution. I think there must be some easy way to do this, so my question is: Suppose I have an x1 = ak.Array([
[1,2,3],
[4,5,6,7],
[8,9],
])
x2 = ak.Array([
[[1,2], [3,4], [5,6]],
[[1,1], [2,2], [3,3], [4,4]],
[[1,2,3], [4,5,6]],
]) If I also have another list (or array) that have the same length to y = [2,0,1] # the column index I want to choose for each row
# i.e., choose column 2 from row 0, choose column 0 from row 1, choose column 1 from row 2 Is there any way to select data so that the output would give me
I have tried something like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @r08222011 What you're describing here is an advanced index. NumPy has its own concept of this (that Awkward supports), e.g. x1[[0, 1,2], [2, 0, 1]] where each array in the subscript operation picks out a component from the corresponding dimension. In other words, [[0, 1,2], [2, 0, 1]] is interpreted as a one dimensional array of [(0, 2), (1, 0), (2, 1)] to build the result. However, this means that the result is always one dimensional. Awkward also has an additional indexing mode, if you pass in a ragged array which has the same number of dimensions as your ix = ak.Array([
[2],
[0],
[1]
])
x1[ix] You can simply slice the result: ix = ak.Array([
[2],
[0],
[1]
])
x1[ix][:, 0] |
Beta Was this translation helpful? Give feedback.
Hi @r08222011
What you're describing here is an advanced index. NumPy has its own concept of this (that Awkward supports), e.g.
where each array in the subscript operation picks out a component from the corresponding dimension. In other words,
is interpreted as a one dimensional array of
N
dimensional tuples:to build the result.
However, this means that the result is always one dimensional.
Awkward also has an additional indexing mode, if you pass in a ragged array which has the same number of dimensions as your
x1
array (i.e. with length-1 lists in the final dimension), it will pick out the values you're interested …