CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Accessing Pandas Series Elements

Pandas Series is a 1-D (One Dimensional) Pandas Data Structure. In the previous post, we have seen how to create a Series Object. Here we will discuss how to access elements of Series in Pandas. There are two ways using which you can access the Individual Series Elements:

  1. By using Data Labels / Index
  2. By using Index Position
  3. By using "at" and "iat" attributes
Syntax:
<Series Object> [ <Valid Index> ]

<Series Object> . at [ <Valid Index> ]

<Series Object> .iat [ <Valid Index position> ]


accessing elements of series pandas



Let us now discuss what is Series Data Labels / Index and Series Index Position. To know the difference between these two terms, check the below given Series student:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import pandas as pd
student = pd.Series(
data = ["BOB", "JHON", "RAM", "MOHAN"],
index = ['S1','S2','S3','S4'])
print(student)

S1      BOB
S2     JHON
S3      RAM
S4    MOHAN
dtype: object

We have created a Series student with data elements as "BOB", "JHON", "RAM" and "MOHAN" and its data labels/index as 'S1', 'S2', 'S3' and 'S4'. Here 'S1', 'S2', 'S3' and 'S4' are called data labels/index of given Series student. Pandas internally maintain a Position for these data labels starting from 0 up to (length - 1) from top and -1 to length from the bottom. You can understand both the terms as below:
 
1
2
3
4
5
Position   Index   Data_Values
  0/-4        S1       BOB
  1/-3        S2       JHON
  2/-2        S3       RAM
  3/-1        S4       MOHAN

Since our Series student has 4 elements, we have positions starting from 0 up to 3. I hope you have now understood the difference between the Series index and index positions.

It is time to discuss the two main types using which we can find the Series elements:

1. By using Data Labels / Index

We will take our previous Series student and syntax mentioned above to find the elements by using Data Labels / Index i.e. 'S1', 'S2', 'S3' and 'S4'. Check the below-given examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> print(student)
S1      BOB
S2     JHON
S3      RAM
S4    MOHAN
dtype: object

>>> student["S1"]
'BOB'

>>> student["S3"]
'RAM'

>>> student["S5"]
## Error 

2. By using Index Positions

Here again, we will use the Series student to find the elements by using Index Positions. The syntax will remain the same as we have used in our previous example.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
>>> print(student)
S1      BOB
S2     JHON
S3      RAM
S4    MOHAN
dtype: object
>>> student[0]
'BOB'

>>> student[-4]
'BOB'

>>> student[2]
'RAM'

>>> student[-2]
'RAM'

>>> student[5]
## Error

3. By using "at" and "iat" attributes

we will use the same series student. "at" and "iat" both are Series attributes, we can use these attributes to find the elements of series.
"at": It takes Data Labels or Index to find the elements
"iat": It takes Index positions to extract the elements from Series

Let check the example of both:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
>>> print(student)
S1      BOB
S2     JHON
S3      RAM
S4    MOHAN
dtype: object
 
>>> student.at['S1']
'BOB'
>>> student.iat[0]
'BOB'

>>> student.at['S4']
'MOHAN'
>>> student.iat[-1]
'MOHAN'

I hope, till now you have learnt how to get / access Series element by index. Now read the below-given questions and try to answer by yourself:

Questions:
  1. How do you access the elements of a Pandas series?
  2. To display the third element of a series object what you will write?
  3. How do you get the first element of the pandas series?
  4. How to get the last element of Series Object?
  5. How to get the second last element of Series Object? 
Answers:
  1. You can access the series elements either using index or index positions.
  2. student[2]
  3. student[0] 
  4. student[-1]
  5. student[-2]



3 comments:

  1. nice explanation..

    ReplyDelete
  2. In the event that you are playing Solo and focusing on evening out this is a wonderful class and you will utilize traps to back off or stop your implore while you downpour down the harm. Best Assignment Help Online

    ReplyDelete
  3. Here at Global Assignment Expert, your online Perdisco accounting assignment help experts provide assignment help in sage 50 assignment help and MYOB Perdisco Accounting. Our team of highly-experienced and well-qualified online Perdisco accounting assignments certify experts in their respective programs and thus, we offer full commitment and plagiarism-free content to global assignments and projects.

    ReplyDelete