0

My question relates to the excellent answer by cs95 in this post right here:

Convert pandas dataframe to NumPy array

It gives a quick and easy method to turn a pandas DataFrame into a numpy-matrix. I hope you forgive me quoting his setup directly:

df = pd.DataFrame(data={'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, 
                  index=['a', 'b', 'c'])

# Convert the entire DataFrame
df.to_numpy()
# array([[1, 4, 7],
#        [2, 5, 8],
#        [3, 6, 9]])

# Convert specific columns
df[['A', 'C']].to_numpy()
# array([[1, 7],
#        [2, 8],
#        [3, 9]])

What I need is to turn the DataFrames columns into a numpy-array. In his answer what we get is an array with the rows.

I would like to be able to get something like this

columns = df.other_to_numpy()
#array([[1,2,3],
#       [4,5,6],
#       [7,8,9]])

Since that would allow me to reference the original columns as columns[0] to columns[2]. I realize this question is very close to the one of the post I am referencing but in the answer to said post I could not find an answer to this variation of the question. Thank you for your patience if this is a very simple problem.

lpnorm
  • 459
  • 3
  • 10

1 Answers1

0

You mean you want the transposed array?

df.T.values
dzang
  • 2,160
  • 2
  • 12
  • 21