How to create a series from a NumPy array?

PandasServer Side ProgrammingProgramming

A pandas Series is very similar to a 1-dimensional NumPy array, and we can create a pandas Series by using a NumPy array. To do this we need to import the NumPy module, as it is a prerequisite for the pandas package no need to install it separately.

It is automatically installed by our package installer. So we can directly import the NumPy module into our workspace.

If you don’t like to use this available version of NumPy, which is installed by the package installer we can install our required version of the NumPy package, as it is also an open-source package like pandas.


import pandas as pd
import numpy as np

array = np.array([8,54,43,6,73,78])
s = pd.Series(array)


In the above example, we have created a pandas Series by using the NumPy array for that purpose we need to import the NumPy module also, initially we imported pandas and NumPy modules with the alias names pd and np respectively.

Then the variable array is created by using the NumPy.array method, this array is having integer values with a length of 6 elements. This array variable is the input data for pandas.Series method.


0    8
1   54
2   43
3    6
4   73
5   78
dtype: int32

The resultant pandas Series is shown in the above output block, here the labels are integer values from 0-5 which are auto-created by the pandas Series method, and the data is also integer data type (int 32).


import pandas as pd
import numpy as np

# create numpy array using array method
array = np.array([1.2,5.5,2.9,4.6])
s = pd.Series(array, index=list('abcd'))

The above block is another example of pandas series created by using NumPy array, where the array is created by all floating-point numbers, and by using this array we will create a pandas Series and the index values are manually created by using string.

Here we have given a list of strings to the index attribute of our pandas Series method.


a   1.2
b   5.5
c   2.9
d   4.6
dtype: float64

This series has 4 elements and each element is a float64 data type. The index values are represented by labels which are a,b,c,d.

Updated on 17-Nov-2021 07:16:46