JoeAA
JoeAA

Reputation: 45

Convert xyz pandas dataframe to multidimensional array

I'm have data of multiple points coordinates like this:

Frame x1 y1 z1 x2 y2 z2 xn yn zn
1 0.1 0.2 0. 3 0.4 0.5 0.6 0.7 0.8 0.9
2 0.2 0.2 0. 3 0.4 0.5 0.6 0.7 0.8 0.9

And i want to convert it to a numpy array so i can plot the points in a surface, so I want to convert each "frame" row to this:

P = [[x1,y1,z1],[x2,y2,z2],[xn,yn,zn]]

and this:

V = [[x1,y1],[x2,y2],[xn,yn]]

Anybody knows how? Thanks

Upvotes: 1

Views: 180

Answers (1)

mozway
mozway

Reputation: 260490

The exact shape of the output should be clarified, but the general logic will be to reshape:

(df.set_index('Frame')
   .to_numpy()
   .reshape((len(df), -1, 3))
)

Output:

array([[[0.1, 0.2, 0.3],   # x1, y1, z1  # first row
        [0.4, 0.5, 0.6],   # x2, y2, z2
        [0.7, 0.8, 0.9]],  # x3, y3, z3

       [[0.2, 0.2, 0.3],   # x1, y1, z1  # second row
        [0.4, 0.5, 0.6],   # x2, y2, z2
        [0.7, 0.8, 0.9]]]) # x3, y3, z3

Upvotes: 4

Related Questions