Creation of a series of pandas



Tags do not have to be unique, but must be hashable. The object supports both integer and label-based indexing and provides many methods for performing index operations. 

Create empty series:
Basic series, which you can create, — this is an Empty Series.

# import pandas as pd

import pandas as pd

 
# Create empty series

ser = pd.Series ()

 

print (ser)

Exit:

 Series ([], dtype: float64) 

Creating a series from an array:
To create a series from an array, we should import the numpy module and use the array () function.

 # import pandas as pd

import pandas as pd

 
# import numpy as np

import numpy as np

 
# simple array

data = np.array ([ `g` , ` e` , `e` , `k` , ` s` ])

 

ser = pd.Series (data)

print (ser)

Output:

Creating a series from an array with an index:
To create a series from an array with an index, we must provide an index with the same element number as in the array.

# import pandas as pd

import pandas as pd

 
# import numpy as np

import numpy as np

 
# simple array

data = np.array ([ `g` , `e` , ` e` , `k` , ` s` ])

 
# providing an index

ser = pd.Series (data, index = [ 10 , 11 , 12 , 13 , 14 ])

print (ser)

Output:

import pandas as pd

 
# simple list

list = [ `g` , ` e` , `e` , ` k`  , `s` ]

  
# create series from the list

ser = pd.Series ( list )

print (ser)

Output:


import pandas as pd

 
# simple dictionary

dict = { `Geeks` : 10 ,

  `for` : 20 ,

  ` geeks` : 30 }

 
# create series from dictionary < / code>

ser = pd.Series ( dict )

 

print (ser)

Output:

Create a series from a scalar value:
To create a series from a scalar value, you must specify an index. The scalar value will iterate to match the length of the index.

import pandas as pd

 

import numpy as np

 
# giving a scalar value with an index

ser = pd.Series ( 10 , index = [ 0 , 1 , 2 , 3 , 4 , 5 ])

  

print (ser)

Output:

Creating a series using numpy functions :
To create a series using numpy function, we can use various numpy functions such as numpy.linspace () ,

# import of pandas and nudies

import pandas as pd 

import numpy as np 

  
# NumPy series Linspace ()

ser1 = pd.Series (np. linspace ( 3 , 33 , 3 )) 

print (ser1) 

 
# series with NumPy Linspace ()

ser2 = pd.Series (np.linspace ( 1 , 100 , 10 )) 

print ( "" , ser2) 

Output: