Ernesto Lopez Fune
Ernesto Lopez Fune

Reputation: 583

Reshape pandas columns into numpy arrays

I have a very long dataframe with many columns of the form k1, p1, k2, p2,...,kn, pn such as

       k1         p1           k2             p2           k3         p3 ...    
-0.001870   0.000659    -0.005000       0.000795    -0.003889   0.000795 ...
-0.002778   0.000556     0.000795       0.001667     0.000795   0.002778 ...

How could I build, in the most pythonic way, a numpy array with the k's and p's separated into arrays, like [[[-0.001870,-0.005000,-0.003889,...],[0.000659,0.000795,0.000795...]],[[-0.002778, 0.000795, 0.000795...],[0.000556, 0.001667, 0.002778...]], ...[[...],[...]]]

Upvotes: 1

Views: 60

Answers (2)

Yilun Zhang
Yilun Zhang

Reputation: 9008

It seems that in your case the columns are alternating between k and p but just in case it's not always the case, you can do:

k_matrix = df[[c for c in df.columns if c.startswith("k")]].to_numpy()
p_matrix = df[[c for c in df.columns if c.startswith("p")]].to_numpy()

Upvotes: 1

mozway
mozway

Reputation: 260420

Convert to_numpy and reshape:

out = df.to_numpy().reshape((len(df), 2, -1), order='F')

Output:

array([[[-0.00187 , -0.005   , -0.003889],
        [ 0.000659,  0.000795,  0.000795]],

       [[-0.002778,  0.000795,  0.000795],
        [ 0.000556,  0.001667,  0.002778]]])

Upvotes: 2

Related Questions