Skip to content
Advertisement

How to read .csv with a compound header into a xarray DataArray (using pandas)

Given a dataset with the following structure:

JavaScript

Given as a .csv:

JavaScript

Note: some values are missing, not all variables are available for all locations, timestamps are available for every record, columns may appear out of order, but timestamp is reliably the first column. I’m not sure all these aspects are relevant to an optimal solution, but there they are.

I didn’t have too much trouble setting up an xarray three dimensional array that would allow me to access values by timestamp, location, variable name. It was looping through the location names after determining the unique ones, filtering the data by location and adding the results one location at a time. But I am wondering what a pythonic and, for lack of a better word, pandastic solution would look like?

Question: Is there some compact and efficient way, likely using pandas and xarray, to load this dataset or any similar one (with different variable and location names) from .csv into a 3d-array like an xarray DataArray?

Advertisement

Answer

JavaScript

However, to get into a 3-D array, we must project this into a cartesian product of the axes available to us.

JavaScript

Then use numpy.reshape and numpy.transpose

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement