Convert a NumPy array to a Pandas series


A Numpy array is an N-dimensional array also called a ndarray, it is a main object of the NumPy library. In the same way, the pandas series is a one-dimensional data structure of the pandas library. Both pandas and NumPy are validly used open-source libraries in python. Below we can see the one-dimensional numpy array.

NumPy array
array([1, 2, 3, 4])

The pandas Series is a one-dimensional data structure with labeled indices and it is very similar to a one-dimensional NumPy array.

Pandas Series:
0    1
1    2
2    3
3    4
4    5
dtype: int64

From the above block we can see the pandas series object, it has 5 integer elements and each element is labeled with a positional index values. In the article below, we will convert a NumPy array to a Pandas Series object.

Input Output Scenarios

Let’s see the input-output scenarios to understand how to convert a NumPy array to a Pandas Series.

Assuming we have a one-dimensional Numpy array with few values, and in the output, we will see a converted pandas Series object from the numpy array.

Input numpy array:
[1 2 3 4]

Output Series:
0    1
1    2
2    3
3    4
dtype: int64

To convert a Numpy array to a Pandas Series, we can use the pandas.Series() method.

The pandas.Series() method

The pandas.Series () method is used to create a Series object based on the given data. The method returns a Series object. Following is the syntax for this –

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

Where,

  • data: Iterable, dict, or scalar value.

  • index: The row labels are specified using this parameter. The default value is 0 to n-1.

  • dtype: This is a string value that is used to specify the data type for the series. (optional)

  • name: This is a string value which specifies a name to the series object. (optional)

  • copy: Copying data from inputs, the default is False.

Example

Let’s convert a NumPy array to a pandas series using the pandas.Series() method.

# importing the modules
import numpy as np
import pandas as pd

# Creating a 1 dimensional numpy array
numpy_array = np.array([1, 2, 8, 3, 0, 2, 9, 4])
print("Input numpy array:")
print(numpy_array)

# Convert NumPy array to Series
s = pd.Series(numpy_array)
print("Output Series:")
print(s)

Output

Input numpy array:
[1 2 8 3 0 2 9 4]
Output Series:
0    1
1    2
2    8
3    3
4    0
5    2
6    9
7    4
dtype: int64

Initially, a one-dimensional numpy array is created by using the integer elements then the array is converted to the pandas Series object.

Example

In this example, the series is converted from a NumPy array of floating-point numbers. While converting, we will use the index parameter to specify row labels for the series object.

# importing the modules
import numpy as np
import pandas as pd

# Creating a 1 dimensional numpy array
numpy_array = np.array([1, 2.8, 3.0, 2, 9, 4.2])
print("Input numpy array:")
print(numpy_array)


# Convert NumPy array to Series
s = pd.Series(numpy_array, index=list('abcdef'))
print("Output Series:")
print(s)

Output

Input numpy array:
[1.  2.8 3.  2.  9.  4.2]
Output Series:
a    1.0
b    2.8
c    3.0
d    2.0
e    9.0
f    4.2
dtype: float64

A list of strings is given to the index parameter of the Series constructor.

Example

In this example, we will convert a two-dimensional numpy array to the series object.

# importing the modules
import numpy as np
import pandas as pd

# Creating a numpy array
numpy_array = np.array([[4, 1], [7, 2], [2, 0]])
print("Input numpy array:")
print(numpy_array)

# Convert NumPy array to Series
s = pd.Series(map(lambda x: x, numpy_array))
print("Output Series:")
print(s)

Output

Input numpy array:
[[4 1]
 [7 2]
 [2 0]]
Output Series:
0    [4, 1]
1    [7, 2]
2    [2, 0]
dtype: object

By using map and lambda functions together, here we have converted the two-dimensional numpy array into a series object. The data type of the converted series is an object type and each series element has an array of integers.

Example

Let’s take another example and convert the 2-dimensional array into a series object.

# importing the modules
import numpy as np
import pandas as pd

# Creating a numpy array
numpy_array = np.array([[4, 1], [7, 2], [2, 0]])
print("Input numpy array:")
print(numpy_array)

# Convert NumPy array to Series
s = pd.Series(map(lambda x: x[0], numpy_array))
print("Output Series:")
print(s)

Output

Input numpy array:
[[4 1]
 [7 2]
 [2 0]]
Output Series:
0    4
1    7
2    2
dtype: int64

Here the series is created by using the 1st-row elements of the 2-dimensional numpy array.

Updated on: 30-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements