KENDRIYA VIDYALAYA NO1 SAMBALPUR
CLASS – XII SUBJECT : INFORMATICS PRACTICES
CHAPTER : DATA HANDLING USING PANDAS - I
Topics to be covered : head(), tail() and Selection in Series
Instructions to the students :
Dear students, in this topic, we are going to discuss about the head(), tail() and Selection in
Series of Python Pandas.
Read and write the following in your classwork notebook.
head() in Series:
It is used to access the first 5 elements of a series.
To access first n elements we can call series_name.head (n) where n is a number.
In Series, head() is used to access the elements of the Series. It is not used to modify the
vales.
For Example : Output :
0 3
1 5
import pandas as pd 2 8
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50]) 3 11
4 46
print([Link]()) # it accesses first 5 elements dtype: int64
print([Link](2)) # it accesses first 2 elements 0 3
1 5
tail() in Series: dtype: int64
It is used to access the last 5 elements of a series.
To access last n elements we can call series_name.tail (n) where n is a number.
In Series, tail() is used to access the elements of the Series. It is not used to modify the vales.
For Example : Output :
k 25
l 71
import pandas as pd m 60
ind=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] n 38
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50], index=ind) o 50
print([Link]()) # it accesses last 5 elements dtype: int64
m 60
print([Link](3)) # it accesses last 3 elements n 38
o 50
Selection in Series : dtype: int64
Series provides three index labels for access series elements in row wise and column wise
manners. These are as follows
1. loc : loc gets elements at particular positions in the index (it takes index as integers only)
2. iloc : iloc gets elements at particular positions in the index (it takes any type of index)
3. [ ] : used for slicing or to select individual elements.
Selection in Series using loc :
Syntax:- series_name.loc[StartRange : StopRange]
StartRange and StopRange are the index labels provided in the Series. The index labels must
be integers only. Output :
For Example: 6 89
15 4
14 25
import pandas as pd 13 71
ind1=[7,8,9,1,2,3,4,5,6,15,14,13,12,11,10] dtype: int64
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50],index=ind1) 7 3
8 5
print([Link][6:13]) 9 8
print([Link][:9]) dtype: int64
Selection in Series using iloc :
Syntax:- series_name.iloc[StartRange : StopRange]
StartRange and StopRange are the index labels provided in the Series. In Series, iloc accesses
starting from StartRange to StopRange – 1. The index labels may be any type.
For Example: Output :
e 46
import pandas as pd f 34
g 78
ind=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] h 21
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50],index=ind) i 89
print([Link][4:9]) dtype: int64
a 3
print([Link][:3]) b 5
c 8
dtype: int64
Selection in Series using [ ] :
Syntax:- series_name [StartRange : StopRange] or series_name [index]
StartRange and StopRange are integers. In Series, [ ] accesses starting from StartRange to
StopRange – 1 and StartRange is always less than equal to StopRange. The index labels of
the Series may be any type, but the StartRange and StopRange are the integers. In Series,
series_name [index] accesses the element in the specified index position as per the index
labels specifies in the Series. Output :
For Example: 2 46
3 34
4 78
import pandas as pd 5 21
ind1=[7,8,9,1,2,3,4,5,6,15,14,13,12,11,10] 6 89
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50],index=ind1) dtype: int64
9 8
print(s1[4:9]) 1 11
print(s1[-13:-10]) 2 46
print(s1[:3]) dtype: int64
7 3
print(s1[1]) 8 5
9 8
dtype: int64
11
Another Example using index labels other than numbers:
Output :
import pandas as pd e 46
f 34
ind=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] g 78
s1= [Link]([3,5,8,11,46,34,78,21,89,4,25,71,60,38,50],index=ind) h 21
print(s1[4:9]) i 89
dtype: int64
print(s1[:3]) a 3
print(s1[1]) b 5
c 8
Assignment : Find the output for the given below code: dtype: int64
5
import pandas as pd
ind=[1205,1203,1209,1201,1202,1221,1215]
s = [Link]([99,95,90,89,85,76,67], index=ind)
print([Link][1209:1202])
print([Link][:1201])
print([Link][1:2])
print([Link][2:5])
print([Link])
print(s[1::4])
Prepared by : Sanatan Banji, PGT [Link]., KV No1 Sambalpur, Mob. No. : 9583825083