How to Convert NumPy Array to Pandas Series?

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

pandas.Series() function is used to convert the NumPy array to Pandas Series. Pandas Series and NumPy array have a similar feature in structure so, we can convert very easily from array to the series. To convert the array to Series we need to import both the NumPy module and Pandas module.

In this article, I will explain pandas.series() function using this how we can convert From NumPy array to Pandas Series. In case, if you want to convert Pandas Series to NumPy array use Series.to_numpy().

1. Quick Examples of Convert Array to Pandas Series

If you are in hurry below are some quick examples of the NumPy Array to Pandas Series conversion.


# Below are some quick examples.

# Example 1: Create a NumPy array using np.zeros()
array = np.zeros(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)

# Example 2: Create a NumPy array numpy.ones()
array = np.ones(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)

# Example 3 : Create a NumPy array using random.rand()
array = np.random.rand(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)

2. Syntax of pandas.Series()

Following is the syntax of the


# Syntax of pandas.Series()
(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

2.1 Parameters

  • 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 Pandas Series.

3. Convert NumPy Array to Pandas Series

NumPy array is a data structure (usually numbers), all of the same type, it looks the same as a list. But arrays are more efficient than Python lists and also much more compact. We can also convert Pandas DataFrame to a 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 the dictionary, and Series to tuple using the Series() method. Let’s create NumPy array using np.zeros() function then, convert array to Series using pd.series() function.


import pandas as pd
import numpy as np

# Create a NumPy array using np.zeros()
array = np.zeros(5)
print("NumPy array:\n", array)

# Convert the NumPy array to a Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))

Yields below output.


# Output:
NumPy array:
 [0. 0. 0. 0. 0.]

Pandas Series:
0    0.0
1    0.0
2    0.0
3    0.0
4    0.0
dtype: float64

3.1 Use ones() Function Create Array & Convert Series.

Here, we can create Numpy array using np.ones() function then, convert array to Series using pandas series() function. It will return the Pandas Series.


# Create a NumPy array numpy.ones()
array = np.ones(5)
print("NumPy array:\n", array)

# Convert the NumPy array to Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))

Yields below output.


# Output:

NumPy array:
 [1. 1. 1. 1. 1.]

Pandas Series:
0    1.0
1    1.0
2    1.0
3    1.0
4    1.0
dtype: float64

3.2 Use numpy. random.rand() Function Create Array & Convert Series.

Use numpy.random.rand() function create Numpy array then, convert array to Series using pd.series() function.


# Create a NumPy array using random.rand()
array = np.random.rand(5)
print("NumPy array:\n", array)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))

Yields below output.


# Output:

NumPy array:
 [0.13894623 0.96695242 0.41112637 0.19469214 0.21145509]

Pandas Series:
0    0.639158
1    0.272269
2    0.402008
3    0.462556
4    0.490533
dtype: float64

Conclusion

In this article, I have explained pandas.series() function using this how we can convert from NumPy array to Pandas Series with multiple examples.

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 How to Convert NumPy Array to Pandas Series?