4

Convert a Pandas Series or Index to a NumPy array

 1 year ago
source link: https://thispointer.com/convert-a-pandas-series-or-index-to-a-numpy-array/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

Advertisements

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()
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)
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

Reema 1
Rekha 2
Jaya 3
['Reema' 'Rekha' 'Jaya']
[1 2 3]
       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)
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)
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']
   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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK