Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert NumPy Array to Pandas Series

  • 28 Dec, 2024
  • Com 0
Transformation from array to series in Python

The most efficient and easiest way to convert a NumPy array to a Pandas Series is by using pd.Series() constructor. It accepts an array as an argument and returns a Series object. Ensure that the array is single-dimensional and not multi-dimensional.

conversion

What is the need for conversion?

  1. The primary difference between the NumPy array and the Pandas series is that NumPy, by default, indexes each value by integer indices. In contrast, in pandas, you can explicitly define labels, which can be of type integer, string, or any other immutable type. Label indexing is more helpful for accessing data from a collection compared to integer indexing.
  2. Pandas objects can easily align the data based on their indices, whereas numpy objects lack this feature.
  3. Series can easily manage NaN values, where Numpy objects have limited support for them.

The process of conversion

When we convert an array to a series, pd.Series() constructor creates a new Series object in memory. It is a different object from an array object.

Then, the data is copied from the array to that object, and now the Series is an independent object with its own data.

While creating a new Series object, if you don’t specify the labels, it will make its own RangeIndex to assign an index to each element.

Transforming NumPy Array to Pandas Series

import numpy as np
import pandas as pd

# Creating an nd array object
numpy_array = np.array([11, 21, 31, 41, 51])

print(numpy_array)
print(type(numpy_array))

# Converting the NumPy array to a Pandas Series
pandas_series = pd.Series(numpy_array)

# Printing the Pandas Series
print(pandas_series)

# Printing the series object type
print(type(pandas_series))

Output

Output of conversion

As shown in the above code, the numpy array’s type is “<class ‘numpy.ndarray’>”, and after the conversion, we have a Series whose type is “<class ‘pandas.core.series.Series’>”.

Converting 2D Numpy Array to Series

You cannot convert a 2D numpy array to a Series because the Series itself is not a multi-dimensional object. A Series is a single-dimensional labeled array.

If you want to get a 2D Pandas object with rows and columns, I recommend converting it into a Pandas DataFrame.

If you are stubborn like me and still want to convert it into a Series, then you have to flatten the 2D array into a 1D array and then use pd.Series() method to get Series.

import numpy as np
import pandas as pd

arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Flatten the array
arr_1d = arr_2d.flatten()

# Convert to Series
series = pd.Series(arr_1d)

print(series)
print(type(series))

Output

Output of converting 2D numpy array to series

That’s all!

Post Views: 56
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert a Numpy Array to String in Python
Pandas DataFrame to_dict() Method

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend