Page 58 - Informatics_Practices_Fliipbook_Class12
P. 58

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
         >>> print(groceryDF.head(5))
                    Product Category  Price  Quantity
              0       Bread     Food     20         2
              1        Milk     Food     60         5
              2     Biscuit     Food     20         2
              3  Bourn-Vita     Food     70         1
              4        Soap  Hygiene     40         4
        Note that the Product column is not appearing as row indexes when the DataFrame is accessed again. It is typical
        of several DataFrame methods, to return a new DataFrame, leaving the original DataFrame unchanged. However, if
        we want that the method should modify the original DataFrame, we must set keyword argument inplace to True:
         >>> groceryDF.set_index('Product', inplace = True)
         >>> print(groceryDF.head(5))
                         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

        Note that the column Product has been set as the index in the dataframe groceryDF.

        2.6.4 Indexing by Integer Indexing
        The integer indexes are used to specify the integer location (iloc) of the rows and columns to be retrieved from a
        Pandas DataFrame. Recall that the integer indexes begin with 0. The syntax for using the iloc attribute is:

        DataFrame.iloc[intRowIndex, intColIndex].
        Here, intRowIndex and intColIndex may be:
              1. a single integer
              2. a list or array of integers
              3. a slice object (e.g., 1:4)
              4. a boolean array
        Thus, to retrieve details of first five purchases, we use the slice [0:5]:

         >>> groceryDF = pd.read_csv('Grocery.csv')
         >>> groceryDF.iloc[0:5]
        output:
                  Product    Category  Price  Quantity
              0   Bread          Food     20         2
              1   Milk           Food     60         5
              2   Biscuit        Food     20         2
              3   Bourn-Vita     Food     70         1
              4   Soap        Hygiene     40         4
        Similarly, in the above example, to retrieve the columns Product (column 0) and Price (column 2), we specify these
        columns as the list [0, 2]:

         >>> groceryDF.iloc[0:5, [0, 2]]



          44   Touchpad Informatics Practices-XII
   53   54   55   56   57   58   59   60   61   62   63