Convert a Pandas Series or Index to a NumPy array

In this article, we will discuss different ways to convert a Pandas Series or Index to a NumPy array in Python.

Table Of Contents

Overview of a Pandas Series

In Pandas, a Series is a one-dimensional data structure which contains values of multiple data types such as integers, objects, and float data types. A Pandas Series is similar to the column of a tabular data structure like DataFrame. Whereas, the axis labels of a DataFrame are collectively called as Index.

What is a NumPy Array in Python?

A NumPy array is a data structure that accepts data of similar types only. NumPy arrays are more efficient than lists and also much more compact.

There are different methods to Convert Pandas Series to NumPy Array. Let’s discuss them one by one.

Convert Pandas Series to NumPy array Using the to_numpy()

In Pandas, the Series.to_numpy() or Index.to_numpy() functions can be used to convert a Series or a Index to a NumPy Array.

Syntax of to_numpy() function

ndarray_object = Index.to_numpy()
ndarray_object = Series.to_numpy()

A pandas script to create DataFrame with one series and convert it to NumPy array using Index.to_numpy() function

import pandas as pd

# create a dataframe
df = pd.DataFrame([1, 2, 3],
                  ['Reema', 'Rekha', 'Jaya'])

# show the dataframe
print(df)

# Convert DataFrame Index to numpy array
array = df.index.to_numpy()

print(array)

seriesObj = df[0]

# Convert DataFrame column / Series to numpy array
array = seriesObj.to_numpy()

print(array)

Output

       0
Reema  1
Rekha  2
Jaya   3
['Reema' 'Rekha' 'Jaya']
[1 2 3]

In the above script, we have used Index.to_numpy() function to convert DataFrame Index to a NumPy Array. Then we used the Series.to_numpy() function to convert a Series to a NumPy Array.

Convert Pandas Index to NumPy array using the Pandas Index.values

Pandas Index is an immutable array used to implementing an ordered, sliceable data structure. It is the basic object which stores the axis labels for all pandas’ objects. The Index.values property will return index array, to convert array into NumPy array we need to use numPy.array() function.

Syntax of Index.values

array = numpy.array(dataFrame.index.values)

Example of pandas.index.values

import pandas as pd
import numpy as np

# create a dataframe
df = pd.DataFrame({ 'Rollno' : [1, 2, 3],
                    'Name' : ['Reema', 'Rekha', 'Jaya'] },
                    index=['a', 'b', 'c'])

# Show the dataframe
print(df)

# Convert DataFrame Index to numpy array
array = np.array(df.index.values)

print(array)

In the above script, we have use Index.values property to change DataFrame Index into one dimensional NumPy array. First we have created a DataFrame with two columns Rollno and Name, then apply numPy.array() function to convert to NumPy Array. The output of above script will contain index values of all three records as one-dimensional array

Output

   Rollno   Name
a       1  Reema
b       2  Rekha
c       3   Jaya

['a' 'b' 'c']

Summary

We learned how to convert a pandas Series or Index to a NumPy array in Python. Happy Learning.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top