Home > OS >  convert xyz var pandas dataframe to numpy 3d grid
convert xyz var pandas dataframe to numpy 3d grid

Time:01-19

I have a dataframe with columns 'x', 'y', 'z', 'v' where x,y and z are the coordinates and v a variable assigned on the point. My dataframe is already regularly spaced in xyz, to be illustrative it's like a big rubix cube but shaped like a 3d rectangle. My spacing between points is set to 1 horizontally and verticaly. I found answers for surfaces but not really for 3d grid data.

I need to convert my dataframe to a 3d (not sure if I can say 4d?) grid in numpy. (I convert to full numpy so I can process my bloc model faster with numba njit)

initial data example :

---------x---y---z---------v

0--------0---0---0--0.375027

1--------0---0---1--0.511405

2--------0---0---2--0.645937

3--------0---0---3--0.229538

4--------0---0---4--0.274867

...------..--..--..------...

160078--26--76--72--0.404251

160079--26--76--73--0.010852

160080--26--76--74--0.048079

160081--26--76--75--0.426528

160082--26--76--76--0.723565

CodePudding user response:

Assuming that there is no missing index and that they are already sorted, it's just a simple reshape:

x, y, z = df[["x", "y", "z"]].max()   1
v = df["v"].to_numpy().reshape((x, y, z))

If your indices have gaps, you can try this. Any missing values will show up as nan:

indices = df[["x", 'y', "z"]].to_numpy().T

v = np.tile(np.nan, indices.max(axis=1)   1)
v[tuple(indices)] = df["v"]
  • Related