Page 27 - Informatics_Practices_Fliipbook_Class12
P. 27
1.5.4 Filtering Series Data
Next, let us consider another example of series comprising numbers that denote marks of students in an examination:
>>> import pandas as pd
>>> marks = [90, 70, 95, 45, 100, 65, 78, 29]
>>> students = pd.Series(marks, index = [101, 102 , 103, 106, 107, 108, 201, 301])
>>> print(students)
output:
101 90
102 70
103 95
106 45
107 100
108 65
201 78
301 29
dtype: int64
Suppose, we need to retrieve the roll numbers of students who have achieved a distinction (i.e. scored at least 75
marks). Pandas provides a simple mechanism to create a series of Boolean values having the same index as the original
series. In the present example, we need to find roll numbers (indexes) of students who have scored marks>=75. So, we
construct a series of Boolean values as follows: for each roll number (index), the corresponding value is set as True if
marks>=75 and False otherwise, as shown below:
>>> boolIndex = (students >= 75)
>>> print(boolIndex)
output:
101 True
102 False
103 True
106 False
107 True
108 False
201 True
301 False
dtype: bool
Next, we retrieve the sub-series comprising only those values for which the index corresponds to the value True, as
shown below:
>>> print(students[boolIndex])
output:
101 90
103 95
107 100
201 78
dtype: int64
Note that only those rows for which the boolean condition holds True are retrieved.
C T 07 1. Write Python code to retrieve ids of those employees earning more than 50000 salary.
2. Use the output obtained in step 1 to retrieve their names.
Data Handling using Pandas 13

