Reputation: 583
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
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