Table of Contents
Converting a Pandas Series to a NumPy array is a simple and efficient way to manipulate and analyze data in Python. To convert a Series to an array, one can use the “.values” attribute, which returns the values of the Series as a NumPy array. For example:
Series = pd.Series([1, 2, 3, 4, 5])
Series.values
# Output: array([1, 2, 3, 4, 5])
This method can also be used to convert a Series of strings to an array of strings:
Series = pd.Series([‘apple’, ‘orange’, ‘banana’])
Series.values
# Output: array([‘apple’, ‘orange’, ‘banana’], dtype=object)
The resulting NumPy array can then be further manipulated and analyzed using various NumPy functions and methods. Overall, converting a Pandas Series to a NumPy array provides a seamless integration between the two libraries, allowing for efficient data analysis and manipulation.
Convert Pandas Series to NumPy Array (With Examples)
You can use the following syntax to convert a pandas Series to a NumPy array:
seriesName.to_numpy()
The following examples show how to use this syntax in practice.
Example 1: Convert Series to NumPy Array
The following code shows how to convert a pandas Series to a NumPy array:
import pandas as pd import numpy as np #define series x = pd.Series([1, 2, 5, 6, 9, 12, 15]) #convert series to NumPy array new_array = x.to_numpy() #view NumPy array new_array array([ 1, 2, 5, 6, 9, 12, 15]) #confirm data type type(new_array) numpy.ndarray
Using the type() function, we confirm that the pandas Series has indeed been converted to a NumPy array.
Example 2: Convert DataFrame Column to NumPy Array
The following code shows how to convert a column in a pandas DataFrame to a NumPy array:
import pandas as pd
import numpy as np
#define DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
'assists': [5, 7, 7, 9, 12, 9, 9, 4],
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
#convert 'points' column to NumPy array
new_array = df['points'].to_numpy()
#view NumPy array
new_array
array([25, 12, 15, 14, 19, 23, 25, 29])
#confirm data type
type(new_array)
numpy.ndarrayWe can use dtype() to check the data type of the new NumPy array as well:
#check data type
new_array.dtype
dtype('int64')
We can see that the new NumPy array is an integer.
Cite this article
stats writer (2024). How can you convert a Pandas Series to a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-you-convert-a-pandas-series-to-a-numpy-array-provide-examples/
stats writer. "How can you convert a Pandas Series to a NumPy array?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-you-convert-a-pandas-series-to-a-numpy-array-provide-examples/.
stats writer. "How can you convert a Pandas Series to a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-you-convert-a-pandas-series-to-a-numpy-array-provide-examples/.
stats writer (2024) 'How can you convert a Pandas Series to a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-you-convert-a-pandas-series-to-a-numpy-array-provide-examples/.
[1] stats writer, "How can you convert a Pandas Series to a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can you convert a Pandas Series to a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
