1

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...]], ...[[...],[...]]]

2 Answers 2

2

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]]])
Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.