-
Hi, I am new to xarray and I am already very impressed by some of the indexing that is accessible. Nevertheless, I am currently facing an issue to fully benefit from the potentiality of the library. I am loading a log file (a big csv file with many columns) that records many parameters in my experiment with pandas which I then convert to a Dataset file. The dataset looks like this; Most of the information gets repeated here, as such I would like to use a different representation. In this representation, I would:
The final output would then look something like this:
I struggled to do it on my own with the API and I was wondering if there exists already some functions to implement this behavior easily or if I should aim for some other ways to treat the data before storing them into the dataset. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
It is not easy to figure this out sadly. from xarray.indexes import PandasMultiIndex
coord_names = ["isOn", "delay2", "delay1"]
(
ds
# drop the existing index variable (required for set_xindex)
.drop_vars("index")
# set the desired new dimensions as coordinate variables
.set_coords(coord_names)
# Assign a PandasMultiIndex
.set_xindex(coord_names, PandasMultiIndex)
# "unstack" to a nD array
.unstack("index")
) |
Beta Was this translation helpful? Give feedback.
-
Fantastic, this works flawlessly! Thank you very much! |
Beta Was this translation helpful? Give feedback.
It is not easy to figure this out sadly.