Coding Ref

How to convert a series to a NumPy array in Pandas

How to convert a series to a NumPy array in Pandas

To convert a Pandas series to a NumPy array, you can use the to_numpy() method. This method will return a NumPy array containing the data from the series.

Here's an example of using the to_numpy() method to convert a Pandas series to a NumPy array:

main.py
import pandas as pd
import numpy as np

# create a sample series
s = pd.Series([3, 2, 1, 4, 5])

# convert the series to a NumPy array
s_array = s.to_numpy()

# display the result
print(s_array)

This will convert the data in the series to a NumPy array, and return a NumPy array containing the data from the series. The resulting array will be displayed to the console.

The output will be:

output
array([3, 2, 1, 4, 5])

Conclusion

The to_numpy() method is a convenient way to convert a Pandas series to a NumPy array. It allows you to easily convert the data in a series to a NumPy array, which can be useful for working with the data in the array.

You'll also like

Related tutorials curated for you

    What is .notnull in Pandas?

    How to drop duplicate rows in Pandas

    How to concatenate in Pandas

    How to give multiple conditions in loc() in Pandas

    How to add an empty column to a Pandas DataFrame

    How to calculate the standard deviation in Pandas DataFrame

    How to use Timedelta in Pandas

    How to reshape a Pandas DataFrame

    How to calculate covariance in Pandas

    What does Head() do in Pandas?

    What is nlargest() in Pandas?

    How to select multiple columns in Pandas