2.1. Creating a Series#

import pandas as pd
import numpy as np

2.1.1. Empty Series#

a=pd.Series()
print(a)
Series([], dtype: float64)
/var/folders/y0/7jpzlk652q19ghz27zs91vt40000gp/T/ipykernel_29767/374528593.py:1: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  a=pd.Series()

2.1.2. From List#

list1=['A','B','C']
print(pd.Series(list1))

# or

print(pd.Series(['A','B','C']))
0    A
1    B
2    C
dtype: object
0    A
1    B
2    C
dtype: object

2.1.2.1. Custom indexes#

a=pd.Series(['A','B','C','D'],index=['First','Second','Third','Fourth'])
print(a)
First     A
Second    B
Third     C
Fourth    D
dtype: object

2.1.3. From numpy array#

# simple array
data = np.array(['S','A','H','I','L'])
a = pd.Series(data)
print(a)
0    S
1    A
2    H
3    I
4    L
dtype: object

2.1.4. From dictionary#

b=pd.Series({
    0:'Sahil',
    1:'Sonia',
    2:'Sourav'
})
print(b)
0     Sahil
1     Sonia
2    Sourav
dtype: object

2.1.5. Assigning indexes after declaring the series#

new_indexes=['a','b','c']
b.index=new_indexes
print(b)
a     Sahil
b     Sonia
c    Sourav
dtype: object