Page 57 - Informatics_Practices_Fliipbook_Class12
P. 57
output:
Product Price Quantity
p1 Bread 20 2
p2 Milk 60 5
We may also retrieve Product, Price, and Quantity for first five purchases using slicing of the form first_label:
last _label:integerIncrement (first_label and last _label inclusive) as follows:
>>> groceryDF.loc['p1': 'p5', ['Product', 'Price', 'Quantity']]
output:
Product Price Quantity
p1 Bread 20 2
p2 Milk 60 5
p3 Biscuit 20 2
p4 Bourn-Vita 70 1
p5 Soap 40 4
Similarly, we may retrieve Product, Price, and Quantity for the three purchases p1, p3, p5, as follows:
>>> groceryDF.loc['p1': 'p5':2, ['Product', 'Price', 'Quantity']]
output:
Product Price Quantity
p1 Bread 20 2
p3 Biscuit 20 2
p5 Soap 40 4
2.6.3 Setting Row Indexes
Sometimes it is convenient to use the contents of a column (often the first column) in a spreadsheet as row indexes.
For example, in the file Grocery.csv, the first column comprises the products purchased by a customer. We may
achieve this by setting the first column as index, while reading the csv file into a DataFrame:
>>> import pandas as pd
>>> groceryDF = pd.read_csv('Grocery.csv', index_col=0)
>>> print(groceryDF)
output:
Category Price Quantity
Product
Bread Food 20 2
Milk Food 60 5
Biscuit Food 20 2
Bourn-Vita Food 70 1
Soap Hygiene 40 4
Brush Hygiene 30 2
Detergent Household 80 1
Tissues Hygiene 30 5
In the above statement, we set index_col = 0 to use the first column as the row labels. It would be interesting to
know that the default value of keyword argument index_col is set to None, which means that row indexes must
begin with 0 and no column is to be used as the row labels.
Alternatively, we may use the set_index() method to explicitly set a column as the index after reading a .csv
file into a DataFrame. Below we first read the file Grocery.csv into the DataFrame groceryDF. Next we set the
Product column as the index of the DataFrame groceryDF:
>>> import pandas as pd
>>> groceryDF = pd.read_csv('Grocery.csv')
>>> groceryDF.set_index('Product')
output:
Product Category Price Quantity
Bread Food 20 2
Data Handling using Pandas DataFrame 43

