'How to make a dictionary from a datset?
I have a dataset AllStations that contains daily temperature data for 225 grid points. I want to turn this into a dictionary where the keys are the dataset index (AllStations.index) and the values are the corresponding temperature values (AllStations.tmax). I.e. I want a dictionary that has 225 keys, from AllStations index and the values should be 25933 corresponding temperature values for each index. How can I do this?
Solution 1:[1]
You could convert the data to a DataFrame and then use the pd.DataFrame.to_dict method:
AllStations.to_dataframe().tmax.unstack('index').to_dict(orient='series')
Alternatively, you could keep things in xarray and convert indices to data variables, which would give you dictionary-like access to each variable:
AllStations.tmax.to_dataset(dim='index')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Delgado |

