Categories
Python Answers

How to convert Python Pandas dataframe to NumPy array?

Spread the love

To convert Python Pandas dataframe to NumPy array, we can use the to_numpy method.

For instance, we write

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

to create the df data frame with some data in it.

Then we call df.to_numpy to return the df data frame as a NumPy object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *