Convert Pandas Series to NumPy Array

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

pandas Series.to_numpy() function is used to convert Series to NumPy array. This function returns a NumPy ndarray representing the values from a given Series or Index. You can also convert Series Index to a numpy array by using Index.array and pandas.index.values properties.

We know that a NumPy array is a data structure (usually numbers), all of the same type, similar to a list. But arrays are more efficient than Python lists and also much more compact hence we will be required to convert Series to array and Pandas DataFrame to a Numpy array. In this article, I will explain how to convert Series to NumPy array using multiple examples.

1. Quick Examples of Convert Pandas Series to Numpy Array

If you are in hurry below are some quick examples of how to convert series to NumPy array.


# Below are some quick examples

# Convert series to numpy array.
import pandas as pd
import numpy as np
Fee = pd.Series([20000, 22000, 15000, 26000, 19000])

# Convert series to numpy array.
new_array = Fee.to_numpy() 

# Convert DataFrame column to numpy 
df = pd.DataFrame({'Courses': ['Java', 'Spark', 'PySpark','Hadoop','C'],
                   'Fee': [15000, 17000, 27000, 29000, 12000],
                   'Discount': [1100, 800, 1000, 1600, 600]
                   },index=['a', 'b', 'c', 'd', 'e'])
new_array = np.array(df.index.values)

# Convert DataFrame column to numpy array.
new_array = df['Discount'].to_numpy() 

# Convert series to numpy using pandas.index.values property.
new_array = np.array(df.index.values)

# Using pandas.index.to_numpy() function.
new_array = df.index.to_numpy()

# Using pandas.index.array property.
new_array = np.array(df.index.array)

2. Syntax of Pandas Series.to_numpy()

Following is the syntax of the Series.to_numpy().


# Syntax of series.to_numpy().
Series.to_numpy(dtype=None, copy=False, na_value=NoDefault.no_default, **kwargs)

2.1 Parameters of the Series.to_numpy()

  • dtype: Data type which we are passing like str.
  • copy : [bool, default False] Ensures that the returned value is a not a view on another array.

2.2 Return Value

It returns NumPy ndarray.

3. Convert Pandas Series to NumPy Array

pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the Pandas Series to list, Series to dictionary, and Series to tuple using the Series() method. In the pandas Series, the row labels of the Series are called the index using this we can access the elements of a Series. The Series can have only one column. A List, NumPy Array, and Dict can be turned into a Series. 

NumPy is a N – Dimensional which stores collection of items of the same type. Items in the collection can be accessed using a zero-based index. Every item in a ndarray takes the same size of a block in the memory. Each element in ndarray is an object of the data-type object (called dtype).

Using pandas Series.to_numpy() function we can convert Series to NumPy array. Let’s create Pandas Series and apply this function. It will return the NumPy ndarray.


# Convert series to numpy array.
import pandas as pd
import numpy as np

fee = pd.Series([20000, 22000, 15000, 26000, 19000])
new_array = fee.to_numpy() 
print(new_array)
print(type(new_array))

Yields below output.


# Output:
[20000 22000 15000 26000 19000]
<class 'numpy.ndarray'>

4. Convert DataFrame Column to NumPy Array

Every column in a DataFrame is represented as Series hence you can convert Pandas DataFrame column to numpy array by using df[column].to_numpy(). Here, df[column] returns a Series.


# Convert DataFrame column to NumPy array.
df = pd.DataFrame({'Courses': ['Java', 'Spark', 'PySpark','Hadoop','C'],
                   'Fee': [15000, 17000, 27000, 29000, 12000],
                   'Discount': [1100, 800, 1000, 1600, 600]},
                    index=['a', 'b', 'c', 'd', 'e'])

new_array = df['Discount'].to_numpy() 
print(new_array)
print(type(new_array))

Yields below output.


# Output:
[1100  800 1000 1600  600]
<class 'numpy.ndarray'>

5. Convert Index to NumPy array

If you want to convert a pandas DataFrame index to a NumPy array, you can use the pandas.Index.values property. The pandas.Index.values property returns the values at the index in the form of an NumPy array. You can also use np.array to convert it to NumPy array.


# Convert DataFrame column to numpy array 
new_array = np.array(df.index.values)
print(new_array)

# Output:
# ['a' 'b' 'c' 'd' 'e']

Here, We first created the Pandas series df with the pd.DataFrame() function. We then converted the df to an array with the df.index.values property and stored it inside the NumPy array array with the np.array() function.

6. Using Pandas.index.to_numpy() function

The pandas.index.to_numpy() function directly converts the values inside the Pandas series to a NumPy array, so we do not need to use the numpy.array()function explicitly. And also a good replacement for the pandas.index.values property is the pandas.index.to_numpy() function.


# Using pandas.index.to_numpy() function.
new_array = df.index.to_numpy()
print(new_array)

# Output:
# ['a' 'b' 'c' 'd' 'e']

7. Using Pandas.index.array property

Another method that can be used in place of the pandas.index.values property is the pandas.index.array property. The pandas.index.array property converts the Pandas series to a array.


# Using pandas.index.array property.
new_array = df.index.array
print(new_array)

# Output:
# ['a' 'b' 'c' 'd' 'e']

Conclusion

In this article, You have learned how to convert the pandas series to a NumPy array by using the Series.to_numpy() function. This function is used to return a NumPy ndarray representing the values from a given Series or Index. You can convert dataframe index to NumPy array by pandas.index.array and pandas.index.values properties.

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Convert Pandas Series to NumPy Array