Merging series of 2D DataFrames to 3D xarray
Question:
I have a series of 2D DataFrames that should be merged into a 3D xarray. The structure of DataFrames looks like this:
5 6 4
8 -1 3
angle
5
105.87
459.62
0.1
10
211.74
919.24
0.1
5 6 4
8 -1 3
angle
5
125.87
439.62
0.2
10
241.74
949.24
0.2
My goal is to have a 3D xarray that will have such structure:
Dimensions: (xyz: 2, thickness: 2, angle: 2)
Coordinates:
* xyz (xyz) object '5 6 4' '8 -1 3'
* thickness (thickness) int 5 10
* angle (angle) float64 0.1 0.2
Data variables:
I don't know how the variables should be sorted
For now I changed DataFrames into xarrays in such a manner:
xa = xarray.Dataset.from_dataframe(df).set_coords("angle")
The 2D xarrays look like this:
Dimensions: (thickness: 2)
Coordinates:
* thickness (thickness) int 5 10
angles (thickness) float64 0.1 0.1
Data variables:
5 6 4 (thickness) float64 105.87 211.74
8 -1 3 (thickness) float64 459.62 919.24
Then when I try to merge the xarrays with .merge
, I got an error MergeError: conflicting values for variable '0 0 0 ' on objects to be combined. You can skip this check by specifying compat='override'.
I wanted to know:
- How to turn angles into a dimension? Seems that it’s something different than coordinates.
- How to make this list of xyz coordinates (‘5 6 4’, ‘8 -1 3’) into another dimension called ‘xyz’?
Answers:
Pandas MultiIndex levels become xarray dimensions and coordinates. DataFrame columns become Dataset variables; series are converted to DataArrays.
So the key is to arrange your data properly in pandas first. You could do this with a number of the pandas reshaping methods, but here’s one:
da = (
df.rename_axis("thickness")
.set_index("angle", append =True)
.rename_axis("xyz", axis=1)
.stack("xyz")
.to_xarray()
)
I have a series of 2D DataFrames that should be merged into a 3D xarray. The structure of DataFrames looks like this:
5 6 4 | 8 -1 3 | angle | |
---|---|---|---|
5 | 105.87 | 459.62 | 0.1 |
10 | 211.74 | 919.24 | 0.1 |
5 6 4 | 8 -1 3 | angle | |
---|---|---|---|
5 | 125.87 | 439.62 | 0.2 |
10 | 241.74 | 949.24 | 0.2 |
My goal is to have a 3D xarray that will have such structure:
Dimensions: (xyz: 2, thickness: 2, angle: 2)
Coordinates:
* xyz (xyz) object '5 6 4' '8 -1 3'
* thickness (thickness) int 5 10
* angle (angle) float64 0.1 0.2
Data variables:
I don't know how the variables should be sorted
For now I changed DataFrames into xarrays in such a manner:
xa = xarray.Dataset.from_dataframe(df).set_coords("angle")
The 2D xarrays look like this:
Dimensions: (thickness: 2)
Coordinates:
* thickness (thickness) int 5 10
angles (thickness) float64 0.1 0.1
Data variables:
5 6 4 (thickness) float64 105.87 211.74
8 -1 3 (thickness) float64 459.62 919.24
Then when I try to merge the xarrays with .merge
, I got an error MergeError: conflicting values for variable '0 0 0 ' on objects to be combined. You can skip this check by specifying compat='override'.
I wanted to know:
- How to turn angles into a dimension? Seems that it’s something different than coordinates.
- How to make this list of xyz coordinates (‘5 6 4’, ‘8 -1 3’) into another dimension called ‘xyz’?
Pandas MultiIndex levels become xarray dimensions and coordinates. DataFrame columns become Dataset variables; series are converted to DataArrays.
So the key is to arrange your data properly in pandas first. You could do this with a number of the pandas reshaping methods, but here’s one:
da = (
df.rename_axis("thickness")
.set_index("angle", append =True)
.rename_axis("xyz", axis=1)
.stack("xyz")
.to_xarray()
)