Page 54 - Informatics_Practices_Fliipbook_Class12
P. 54

By default, these methods return five rows of the DataFrame. Let us make use of these methods to display the details
        of first/last five products purchased from the grocery store using the DataFrame groceryDF.
         >>> print("Details of first 5 products purchased from grocery store:\n" ,groceryDF.head())
              Details of first 5 products purchased from grocery store:
                    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
         >>> print("Details of last 5 products purchased from grocery store:\n", groceryDF.tail() )
              Details of last 5 products purchased from grocery store:
                    Product   Category  Price  Quantity
              3  Bourn-Vita       Food     70         1
              4        Soap    Hygiene     40         4
              5       Brush    Hygiene     30         2
              6   Detergent  Household     80         1
              7     Tissues    Hygiene     30         5
        However, one can also specify the desired number of the rows as an input argument to the methods head() and
        tail(), as shown below:

         >>> nProducts = 3
         >>> print("\nDetails of first", nProducts, "products:\n",groceryDF.head(nProducts))
              Details of first 3 products:
                 Product Category  Price  Quantity
              0    Bread     Food     20         2
              1     Milk     Food     60         5
              2  Biscuit     Food     20         2
         >>> print("\nDetail of last", nProducts, "products:\n",groceryDF.tail(nProducts))
              Detail of last 3 products:
                   Product   Category  Price  Quantity
              5      Brush    Hygiene     30         2
              6  Detergent  Household     80         1
              7    Tissues    Hygiene     30         5

          C T  03     1.  Consider the following DataFrame employeeDF:

                         ID             Name          Department     Salary
                         E1             Sid           Admin          90000
                         E2             Ram           Accounts       1000000
                         E3             Sita          Admin          90000
                         E4             Shyam         Accounts       1000000
                         Write Python statements to display details of the first two employees working for the company.





               head(n): Returns first n rows of a DataFrame.
               head(): Returns first 5 rows of a DataFrame.
               tail(n): Returns last n rows of a DataFrame.
               tail(): Returns last 5 rows of a DataFrame.







          40   Touchpad Informatics Practices-XII
   49   50   51   52   53   54   55   56   57   58   59