Page 55 - Informatics_Practices_Fliipbook_Class12
P. 55
2.6.1 Retrieving Columns: Indexing by Column Name
Sometimes, we may be interested in knowing only the names of the products in rather than the complete details of the
products. The names of the products in the column Product can be retrieved using Product as the column index:
>>> productNames = groceryDF['Product']
>>> print(productNames)
output:
0 Bread
1 Milk
2 Biscuit
3 Bourn-Vita
4 Soap
5 Brush
6 Detergent
7 Tissues
Name: Product, dtype: object
The above process of selecting a subset of data in a DataFrame is known as indexing. Note that by default, a Pandas
DataFrame is indexed by columns. Further, note that it is in contrast to series, where indexes correspond to row labels.
We may also access a column (say, Product) as an attribute of groceryDF, as shown below:
>>> groceryDF.Product
output:
0 Bread
1 Milk
2 Biscuit
3 Bourn-Vita
4 Soap
5 Brush
6 Detergent
7 Tissues
Name: Product, dtype: object
Suppose, we wish to retrieve the product purchased and their prices. For this purpose, we specify the names of
attributes Product and Price of the DataFrame groceryDF in the form of a list, as shown below:
>>> groceryDF[['Product', 'Price']]
output:
Product Price
0 Bread 20
1 Milk 60
2 Biscuit 20
3 Bourn-Vita 70
4 Soap 40
5 Brush 30
6 Detergent 80
7 Tissues 30
2.6.2 Indexing by Row Label
Sometimes, We need to access the details of a particular product, say, the third product, in DataFrame. For this
purpose, we specify that we are interested in the details of the product at location 2 (row indexes begin with 0), as
illustrated below:
>>> groceryDF.loc[2]
output:
Product Biscuit
Category Food
Price 20
Quantity 2
Name: 2, dtype: object
Data Handling using Pandas DataFrame 41

