Create series of pandas from array

Let's see how to create a Pandas series from an array.

Method # 1: Create a series from an array without an index.

In this case, because no index is passed, by default the index will be range (n) where n — array length.

# pandas and nampi imports

import pandas as pd

import numpy as np

 
# numpy array

data = np.array ([ 'a' , 'b' , ' c' , 'd' , 'e' ])

  
# create series

s = pd.Series (data)

print (s)

Exit:

 0 a 1 b 2 c 3 d 4 e dtype: object 

Method # 2: Create a series from an array with an index.

In this in this case we will pass the index as a parameter to the constructor.

# pandas and nampi imports

import pandas as pd

import numpy as np

 
# numpy array

data = np.array ([ ' a' , 'b' , 'c' , ' d ' , ' e' ])

 
# create series

s = pd.Series (data, index = [ 1000 , 1001 , 1002 , 1003 , 1004 ])

print (s)

Exit :

 1000 a 1001 b 1002 c 1003 d 1004 e dtype: object 




Get Solution for free from DataCamp guru