Page 76 - Informatics_Practices_Fliipbook_Class12
P. 76

<class 'tuple'> 2 Food <class 'pandas.core.frame.DataFrame'>
              <class 'tuple'> 2 Household <class 'pandas.core.frame.DataFrame'>
              <class 'tuple'> 2 Hygiene <class 'pandas.core.frame.DataFrame'>
        Now we are ready to display the information contained in the grouped DataFrame groceryGroupedDF group by
        group:
         >>> for records in groceryGroupedDF:
                print("\nCategory:", records[0])
                print("Items Purchased")
                print(records[1])
              Category: Food
              Items Purchased
                    Product Category  Price  Quantity  Total Price
              0       Bread     Food     20         2           40
              1        Milk     Food     60         5          300
              2     Biscuit     Food     20         2           40
              3  Bourn-Vita     Food     70         1           70

              Category: Household
              Items Purchased
                   Product   Category  Price  Quantity  Total Price
              6  Detergent  Household     80         1           80

              Category: Hygiene
              Items Purchased
                 Product Category  Price  Quantity  Total Price
              4     Soap  Hygiene     40         4          160
              5    Brush  Hygiene     30         2           60
              7  Tissues  Hygiene     30         5          150
        Before closing our discussion of Pandas, we would like to introduce our readers to the Pandas tutor available at https://
        pandastutor.com/vis.html. Pandas tutor lets us visualizes how Pandas code transforms DataFrames on sample dataset.
        The tool provides some insightful visualization of data, even though it has some limitations such as:
        1.  Pandastutor is limited to handling small code files (up to 5000 bytes) and can only visualize small datasets.
        2.   Data  must  be  encoded  within  the  code  itself,  as  external  resource  reading  (such  as  csv or  txt  files)  is  not
            supported.
        3.  It offers limited Pandas methods support and allows visualization only on the last line.
        Below, we provide the pandas tutor visualization for the grouped records:

                    Product   Category  Price  Quantity  Total Price
              0       Bread       Food     20         2           40
              1        Milk       Food     60         5          300
              2     Biscuit       Food     20         2           40
              3  Bourn-Vita       Food     70         1           70
              6   Detergent  Household     80         1           80
              4        Soap    Hygiene     40         4          160
              5       Brush    Hygiene     30         2           60
              7     Tissues    Hygiene     30         5          150
        2.10.2 Aggregation

        As we have grouped the products based on their category, we can compute the total price paid for all the items taken
        together that have the same Category value using the GroupBy object groceryGroupedDF created above and
        applying the aggregate method sum() on the column Total Price as shown below:
         >>> #Total expenditure per category
         >>> groceryGroupedDF['Total Price'].sum()



          62   Touchpad Informatics Practices-XII
   71   72   73   74   75   76   77   78   79   80   81