-1

I have 3 columns in my df

enter image description here

How can i convert this to a matrix? I tried

numpy_matrix=df.as_matrix()

but this doenst give a matrix

3
  • 3
    use df.values ? Commented Mar 4, 2019 at 14:52
  • Don't use a matrix, they have a special meaning with numpy and their usage is not advised. Commented Mar 4, 2019 at 15:10
  • I need to perform matrix algebra after this step Commented Mar 4, 2019 at 15:11

2 Answers 2

1

To convert a pandas dataframe (df) to a numpy ndarray, use this code:

df = df.values

df now becomes the numpy ndarray:

Sign up to request clarification or add additional context in comments.

Comments

1

In pandas 0.24.0+, it is recommended that you use to_numpy. The function as_matrix was deprecated in pandas 0.23.0.

df.to_numpy()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.