How to convert a pandas Series or index to a Numpy array?

Sometimes, we want to convert a pandas Series or index to a Numpy array.

In this article, we’ll look at how to convert a pandas Series or index to a Numpy array.

How to convert a pandas Series or index to a Numpy array?

To convert a pandas Series or index to a Numpy array, we can use the to_numpy method.

For instance, we write

a = df.index.to_numpy()
b = df['A'].to_numpy()

to call df.index.to_numpy to convert the df data frame’s index into a NumPy array.

Likewise, we call df['A'].to_numpy to convert the values in column A in the df data frame into a NumPy array.

Conclusion

To convert a pandas Series or index to a Numpy array, we can use the to_numpy method.