0% found this document useful (0 votes)
4 views2 pages

Xii Ip Module3

The document covers the topic of accessing elements in a Series using Python Pandas, including methods for Series indexing and slicing. It provides examples demonstrating how to access individual elements and subsets of a Series, along with the syntax for slicing. Additionally, there is an assignment at the end for students to find the output of given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Xii Ip Module3

The document covers the topic of accessing elements in a Series using Python Pandas, including methods for Series indexing and slicing. It provides examples demonstrating how to access individual elements and subsets of a Series, along with the syntax for slicing. Additionally, there is an assignment at the end for students to find the output of given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KENDRIYA VIDYALAYA NO1 SAMBALPUR

CLASS – XII SUBJECT : INFORMATICS PRACTICES


CHAPTER : DATA HANDLING USING PANDAS - I
Topics to be covered : Accessing elements of a Series

Instructions to the students :


Dear students, in this topic, we are going to discuss about Series of Python Pandas and
different ways to create Series.
Read and write the following in your classwork notebook.

Accessing elements of a Series:


Each element of a Series can be accessed using Series Indexing and Series Slicing.
Accessing individual elements using Series Indexing :
For Series, using indexes is straightforward. That is, <Series>[<index>] is the required
element. For example, try the following codes to access elements of a Series.
Example 1: Output :
import pandas as pd 5
arr=[5, 15, 25, 35, 50] # List arr is created 15
25
s1= pd.Series(arr) # Series s1 is created using List arr
35
for a in range(5): 50
print(s1[a]) # Each element of the Series s1 is accessed s1[a]

Example 2: Accessing each element of the Series using default index Output :
import pandas as pd 5
import numpy as np 15
arr=np.array([5, 15, 25, 35,50]) 25
s1= pd.Series(arr, index=['First','Second','Third','Fourth','Fifth']) 35
for a in range(5): 50
print(s1[a])

Example 3: Accessing each element of the Series using customized index


Output :
import pandas as pd
5
import numpy as np 15
arr=np.array([5, 15, 25, 35,50]) 25
ind=['First','Second','Third','Fourth','Fifth'] 35
s1= pd.Series(arr, index=ind) 50
for a in range(5):
print(s1[ind[a]]) # each element of ind[a] is the customized index of the Series s1

Example 4: Accessing elements of Series (using default index) created using dictionary
import pandas as pd
Output :
d1={'RollNo':1201,'Name':'Ramesh','Mark':99}
1201
s1= pd.Series(d1) Ramesh
for a in range(3): 99
print(s1[a])

Example 5: Accessing elements of Series (customized index) created using dictionary


import pandas as pd
d1={'RollNo':1201,'Name':'Ramesh','Mark':99} Output :
s1= pd.Series(d1)
print(s1[‘RollNo’]) 1201
Ramesh
print(s1['Name'])
Accessing individual elements using Slicing in Series :
Slicing is a way to retrieve subsets of data from a pandas object.
Slicing in Series refers to the process of extracting a subset of elements from an existing
Series and returning the result as another Series, possibly in a different dimension from the
original.
Slice object syntax is –
SERIES_NAME [start: end: step]
The segment start represents the first item; end represents the last item and step represents the
increment between each item that you would like.
For example :
Output :
import pandas as pd
2nd 15
import numpy as np 4th 25
arr=np.array([5, 15, 20, 25, 32, 38, 50,60]) 6th 38
s1= pd.Series(arr, index=['1st','2nd','3rd','4th','5th','6th','7th','8th']) dtype: int32
print(s1[1:6:2])

Given Series S= pd.Series([5, 15, 20, 25, 32, 38, 50, 60, 65, 80])
Series Slice Description Example
S[n : m] Extract series slice from n to m – 1 S[3:7] extracts 25, 32, 38, 50
S[ : m] Extract series slice from 0 to m – 1 S[ : 4] extracts 5, 15, 20, 25
S[n : ] Extract series slice from n to the end S[6:] extracts 50, 60, 65, 80
S[n : –1 ] Extract series slice from n to end – 1 S[6: –1] extracts 50, 60, 65
S[n : –2] Extract series slice from n to end – 2 S[4: –2] extracts 32, 38, 50, 60
S[n : m : k] Extract series slice from n to m – 1 S[1:7:2] extracts 15, 25, 38
th
picking every k element
S[ : : –1] Extract series slice in reverse way S[ : : –1] extracts 80,65,60,50,
38,32,25,20,15,5
S[ –n : –m] Extract series slice from nth from last S[–6: –2] extracts 32, 38, 50, 60
to end – m
S[ : : k] Extract series slice from first to last S[ : : 3] extracts 5, 25, 50 ,80
th
picking every k element
S[–n : ] Extract series slice from nth from last S[–3 : ] extracts 60, 65, 80
to end
S[–n:–m:–1] Extract series slice from nth from last S[–2:–6:–1] extracts 65, 60, 50, 38
to m–1th from last in reverse order

Assignment : Find the output for the given below code:


import pandas as pd
s = pd.Series([1,2,3,4,5,6,7], ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])
print(s[ : :2])
print(s[ : :–1])
print(s[1: :2])
print(s[–5: –3])
print(s[–3:])
print(s[2:5])
print(s[ : : –1])
print(s[–2: –5: –1])
print(s[‘Thu’])

Prepared by : Sanatan Banji, PGT Comp.Sc., KV No1 Sambalpur, Mob. No. : 9583825083

You might also like