Revision Point - Series
Revision Point - Series
Page2
6|K V S
REGIONAL OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION
2021-22)
Series_object. It show the data types of data Apr 30
dtype values of series object dtype: int64
Series_object. It show tuple of shape print( Sr_Obj['Feb']) 28
shape underlying data of series object print(Sr_Obj['Apr']) 30
Series_object. It show the number of bytes of Accessing Slice of Series
nbytes underlying data of series object Slicing takes place position wise (built in Index)
Series_object. It show the number of and not the index wise in a series object.
ndim dimensions of underlying data Syntax: Series_Object[Start: End: Step]
of series object Where,
Series_object. It show the number elements in Start is Lower Limit (default is 0)
size series object End is Upper Limit
Series_object. It show the size of data type of Step is updation (default is 1)
itemsize underlying data of series object Note: slicing may be ve also
Series_object. It show True if there is NaN / print(Sr_Obj[1:3:1]) Feb 28
hasnans None value in Series, otherwise Mar 31
returns False. print(Sr_Obj[-1:-3:-1]) Apr 30
Series_object. It returns True if series is Mar 31
empty empty, otherwise returns False. print(Sr_Obj[1::]) Feb 28
Mar 31
Example with Attribute Output Apr 30
# Example of Series print(Sr_Obj[::1]) Jan 31
import numpy as np Feb 28
import pandas as pd Mar 31
Apr 30
Val=[31,28,31,30] print(Sr_Obj[::-1]) Apr 30
Sr_Obj=pd.Series(data=Val, index=Ind) Mar 31
Feb 28
print(Sr_Obj.index) Index(['Jan', 'Feb', Jan 31
'Mar', 'Apr'], Modifying Elements of of Series
dtype='object')
print(Sr_Obj.values) [31 28 31 30] Syntax: Series_Object[index / slice]= new value
print(Sr_Obj.dtype) int64 Sr_Obj[1]=29 Jan 31
print(Sr_Obj.itemsize) 8 print(Sr_Obj) Feb 29
print(Sr_Obj.size) 4 Mar 31
print(Sr_Obj.ndim) 1 Apr 30
print(Sr_Obj.empty) False Sr_Obj[:-3:-1]=31 Change 31 in Last
print(Sr_Obj.hasnans) False print(Sr_Obj) 2 place
print(Sr_Obj.nbytes) 32 Jan 31
print(Sr_Obj.shape) (4,) Feb 29
Mar 31
Accessing individual element of Series Apr 31
print("Add New element Add New element
Syntax: Series_Object[Valid index] 100") 100
import numpy as np Sr_Obj['May']=100 Jan 31
import pandas as pd print(Sr_Obj) Feb 29
Mar 31
Val=[31,28,31,30] Apr 31
Sr_Obj=pd.Series(data=Val, index=Ind) May 100
# print Whole series Jan 31 print("Delete Last index") Delete Last index
Feb 28 del Sr_Obj['May'] Jan 31
print(Sr_Obj) Mar 31 print(Sr_Obj) Feb 29
Page 3
7|K V S R E G I O N A L O F F I C E , J A I P U R | S U B J E C T - I N F O R M A T I C S P R A C T I C E S ( T E R M - I S E S S I O N
2021-22)
Mar 31 print(Sr_Obj.tail(6)) Mar 31
Apr 31 Apr 30
print("Rename Index") Rename Index May 31
Sr_Obj.index=['J','F','M','A'] J 31 Jun 30
print(Sr_Obj) F 29 Jul 31
M 31 Vector operations on Series Object
A 31 Similar to nd-array, the vector operations can be
applied on series object also. Scalar operation
mean, one operation can be applied to each
head( ) and tail( ) element of series object at a time.
import pandas as pd
head( ) returns first n rows and tail( ) returns import numpy as np
last n rows from series.
If n is not given then by default it will return 5 data=[10,20,30,40])
rows.
Sytax: print("Add 5 in each element of Add 5 in each
Series_Object.head([n]) Sr_Obj") element of
Series_Object.tail([n]) print(Sr_Obj+5) Sr_Obj
import numpy as np A 15
import pandas as pd B 25
Ind=['Jan','Feb','Mar','Apr','May','Jun','Jul'] C 35
Val=[31,28,31,30,31,30,31] D 45
Sr_Obj=pd.Series(data=Val, index=Ind) print("Multiply by 5 in each Add 5 in each
element of Sr_Obj") element of
print("Display First 2 Display First 2 Rows print(Sr_Obj*5) Sr_Obj
Rows") Jan 31 A 50
print(Sr_Obj.head(2)) Feb 28 B 100
print("Display First 5 Display First 5 Rows C 150
Rows") Jan 31 D 200
print(Sr_Obj.head()) Feb 28 print("Divide 5 in each element Add 5 in each
Mar 31 of Sr_Obj") element of
Apr 30 print(Sr_Obj/5) Sr_Obj
May 31 A 2.0
print("Display First 6 Display First 6 Rows B 4.0
Rows") Jan 31 C 6.0
print(Sr_Obj.head(6)) Feb 28 D 8.0
Mar 31 print(Sr_Obj>20) A False
Apr 30 B False
May 31 C True
Jun 30 D True
print("Display Last 2 Display Last 2 Rows print("Sr_Obj**2") A 100
Rows") Jun 30 print(Sr_Obj**2) B 400
print(Sr_Obj.tail(2)) Jul 31 C 900
print("Display Last 5 Display Last 5 Rows D 1600
Rows") Mar 31 #Adding two Series of similar indexes
print(Sr_Obj.tail()) Apr 30 import numpy as np
May 31 import pandas as pd
Jun 30 class11=pd.Series(data=[30,40,50],index=['scien
Jul 31 ce','arts','commerce'])
print("Display Last 6 Display Last 6 Rows class12=pd.Series(data=[60,80,100],index=['scie
Rows") Feb 28 nce','arts','commerce'])
8Page
|K V4
S REGIONAL OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION
2021-22)
print("Total number of students") D 100
print(class11+class12) Sorting Series Values based on Indexes
Sr_Obj.sort_index() OR A 200
Output: Sr_Obj.sort_index(ascending= B 400
Total number of students True) C 300
science 90 D 100
arts 120 Sr_Obj.sort_index(ascending= D 100
commerce 150 False) C 300
#Adding two Series of dissimilar indexes B 400
class11=pd.Series(data=[30,40,50],index=['scien A 200
ce','arts','commerce']) Arithmetic on Series
class12=pd.Series(data=[60,80,100],index=['sci',
import pandas as pd Addition of
'arts','commerce'])
import numpy as np Series-s1+s2:
print("Total number of students")
s1=pd.Series(data=[20,40,60], A 22
print(class11+class12)
index=['A','B','C']) B 44
s2=pd.Series(data=[2,4,6], C 66
Output:
index=['A','B','C'])
Total number of students
print("Addition of Series:
arts 120.0
s1+s2")
commerce 150.0
print(s1+s2)
sci NaN
print("Division of Series: s1/s2") Division of
science NaN
print(s1/s2) Series: s1/s2
A 10.0
Filtering Entries of Series
B 10.0
import pandas as pd
C 10.0
Info=pd.Series(data=[31,41,51]) info>40
0 False
print("Addition of Series: Addition of
p \ 1 True
S3=s1+s2") Series:
2 True
s3=s1+s2 S3=s1+s2
p \ info[info>40]
print(s3) A 22
info[info>40]) 1 41
B 44
2 51
C 66
Sorting Series Values based on Values NumPy Arrays Vs Series Object
import pandas as pd D 100
1. In ndarray, vector operations can only be
import numpy as np A 200
performed if shape of both array match,
C 300
otherwise it will generate error.
B 400
2. In Series, vector operations can have
data=[200,400,300,100]) (By default
performed with different shapes series
Sr_Obj.sort_values() OR order is
also. For different shape series operation
Sr_Obj.sort_values(ascending= Ascending)
gives NaN values.
True)
3. In ndarray, the indexes always numeric
and start with 0 onwards. But in series,
Sr_Obj.sort_values(ascending= B 400 indexes can have any type of indexes.
False) C 300
A 200
Page
9 | K V S 5R E G I O N A L
2021-22)
OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION