Page 59 - Informatics_Practices_Fliipbook_Class12
P. 59
output:
Product Price
0 Bread 20
1 Milk 60
2 Biscuit 20
3 Bourn-Vita 70
4 Soap 40
Method loc: Access elements in a Pandas DataFrame using label-based indexing.
Method iloc: Access elements in a Pandas DataFrame using integer positional indexing.
The keyword argument index_col: Set a column as the row labels.
Method pd.set_index(): Set a column as the index after reading a .csv file into a DataFrame.
Consider the following DataFrame storing employee details:
ID Name Department Salary
E1 Sid Admin 90000
E2 Ram Accounts 1000000
E3 Sita Admin 90000
E4 Shyam Accounts 1000000
(i) Retrieve the contents of column ID and Department.
(ii) Determine the salary of second employee.
(iii) Set column ID as the index labels.
2.6.5 Boolean Indexing
Boolean indexing is a powerful technique for filtering data from a Pandas DataFrame based on some condition.
To Retrieve Certain Rows
With boolean indexing, you can filter a DataFrame to include only rows that satisfy a certain condition. To use Boolean
indexing, we first mark the rows of our interest as True and the other rows as False, based on some condition. This
process of marking the rows as True and False is known as masking, and the Boolean DataFrame of True and
False is known as a mask. A boolean mask is a Pandas series of the same length as the original DataFrame. Below, we
create a mask for marking the rows in the groceryDF DataFrame, for which the price of a product is more than 50:
>>> import pandas as pd
>>> groceryDF = pd.read_csv('Grocery.csv')
>>> mask = groceryDF['Price'] > 50
>>> print(mask)
>>> type(mask)
output:
0 False
1 True
2 False
3 True
4 False
5 False
6 True
7 False
Name: Price, dtype: bool
pandas.core.series.Series
Data Handling using Pandas DataFrame 45

