Page 56 - Informatics_Practices_Fliipbook_Class12
P. 56

Note that by default, row indexes begin with integer 0, thus, we have used row label 2 as the index. However, you
        might prefer to address the products as p1, p2, ... . For this we construct a list of row indexes (row labels) as p1, p2, ...,
        as explicit labels, as shown below:
         >>> import pandas as pd
         >>> groceryDF = pd.read_csv('Grocery.csv')
         >>> rowLabels = []
         >>> for i in range(len(groceryDF)):
                rowLabels.append('p'+str(i+1))
         >>> print(rowLabels)
        output:
              ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8']
        Now we use the list rowLabels for indexing the DataFrame groceryDF:

         >>> groceryDF.index= rowLabels
         >>> print(groceryDF.head(5))
        output:
                     Product Category  Price  Quantity
              p1       Bread     Food     20         2
              p2        Milk     Food     60         5
              p3     Biscuit     Food     20         2
              p4  Bourn-Vita     Food     70         1
              p5        Soap  Hygiene     40         4
        Now we are ready to use row labels created above as indexes. Suppose, we wish to access the details about a purchase,
        say, the third purchase. For this purpose, we use the attribute loc of the DataFrame groceryDF to specify the
        location p3 of the third row in the DataFrame:
         >>> groceryDF.loc['p3']
        output:
              Product     Biscuit
              Category       Food
              Price            20
              Quantity          2
              Name: p3, dtype: object
        Next, suppose, we wish to retrieve the name of the product bought in the third purchase. This may be achieved by
        providing the column label (index) 'Product' along with the row label, as shown below:
         >>> groceryDF.loc['p3', 'Product']
        output:
              'Biscuit'
        Now we give the syntax for using the loc attribute:

        DataFrame.loc[rowIndex, columnIndex]
        Note that the rowIndex refers to row label(s) and columnIndex refers to column label(s). The rowIndex and
        columnIndex may refer to:

              1. A single label
              2. A list or array of labels
              3. A slice object (e.g. 'label1':'label2')
              4. A boolean array

        Suppose, we need to determine Product, Price, and Quantity for the first two purchases from the grocery
        store. For this purpose, we expres the row indexes as the list [p1, p2]. Similarly, we express the column indexes as
        the list ['Product', 'Price', 'Quantity']. Now the required details can be retrieved as follows:

         >>> groceryDF.loc[['p1', 'p2'], ['Product', 'Price', 'Quantity']]



          42   Touchpad Informatics Practices-XII
   51   52   53   54   55   56   57   58   59   60   61