Transpose a part of an Array #945
-
Hello everyone, I'm trying to get the particle track data from the .root files with the The
and I want to get a structure that is
I suppose it would be more clear with an example. I've got
and I want to get
So not only I want to transpose, I want to
I've tried the most obvious thing - the
But got the following error:
So what would be the most appropriate way to do this transformation? I'm doing it so I can write the resulting array as a 'hit_variables' column of the pandas dataset to do some more transformations and write it to the .hdf file. This is only the part of the whole transformation process which is quite memory consuming and I'm to to this python way to handle big datases so I want to make sure I would create as little intermediate data as possible. Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Given that you're using uproot, I believe you're obtaining arrays of the form >>> arr = ak.Array([
... { 'hit_x' : [ 0, 1, 0],
... 'hit_y' : [12, 0.3, 7],
... 'hit_z': [3, 8, 6]},
...
... {'hit_x' : [ 1, 1],
... 'hit_y' : [3, 7],
... 'hit_z': [0,4]}
... ]) If you just wanted the records to have scalar fields, then one way to achieve what you want is to re-zip the array, i.e. >>> fields = ak.unzip(arr)
>>> names = ak.fields(arr)
>>> rezipped = ak.zip(dict(zip(names, fields)))
>>> rezipped.tolist()
[[{'hit_x': 0, 'hit_y': 12.0, 'hit_z': 3},
{'hit_x': 1, 'hit_y': 0.3, 'hit_z': 8},
{'hit_x': 0, 'hit_y': 7.0, 'hit_z': 6}],
[{'hit_x': 1, 'hit_y': 3.0, 'hit_z': 0},
{'hit_x': 1, 'hit_y': 7.0, 'hit_z': 4}]] Then you can unzip the records >>> ak.unzip(rezipped)
(<Array [[0, 1, 0], [1, 1]] type='2 * var * int64'>,
<Array [[12, 0.3, 7], [3, 7]] type='2 * var * float64'>,
<Array [[3, 8, 6], [0, 4]] type='2 * var * int64'>) However, you are asking about how to concatenate the fields as columns. To do this, we do not need to re-zip the array. First, we need to unzip the original array: >>> x, y, z = ak.unzip(arr) What we want to do is call >>> new_dim = (..., np.newaxis)
>>> transposed = ak.concatenate((x[new_dim], y[new_dim], z[new_dim]), axis=-1)
>>> transposed.tolist()
[[[0.0, 12.0, 3.0], [1.0, 0.3, 8.0], [0.0, 7.0, 6.0]],
[[1.0, 3.0, 0.0], [1.0, 7.0, 4.0]]] Note that this will create intermediate data because There might be a faster way to do this (if that matters), depending upon how the data are serialised & deserialised; if |
Beta Was this translation helpful? Give feedback.
Given that you're using uproot, I believe you're obtaining arrays of the form
If you just wanted the records to have scalar fields, then one way to achieve what you want is to re-zip the array, i.e.