Page 69 - Informatics_Practices_Fliipbook_Class12
P. 69

For  the  sake  of  convenience,  we  have  used  the  same  name  (TotalPrice)  for  the  column  to  be  added  to  the
            Dataframe and the variable name that was used to construct the series by taking row wise products of values in the
            columns groceryDF['Price'] and groceryDF['Quantity'].

            Modifying an existing column
            Pandas also allows us to modify the contents of an existing column in the DataFrame. For example, suppose, that the
            grocery store allows a flat discount of five rupees on the purchse of each item, irrespective of the quantity of the item
            purchased. The assignment statement.

            groceryDF['TotalPrice'] = groceryDF['TotalPrice'] - 5
            achieves this, as shown below:
             >>> # Adding a new Column to the Dataframe
             >>> groceryDF['TotalPrice'] = groceryDF['TotalPrice'] - 5
             >>> print(groceryDF.head())
                       Product Category  Price  Quantity  TotalPrice
                 0       Bread     Food     20         2          35
                 1        Milk     Food     60         5         295
                 2     Biscuit     Food     20         2          35
                 3  Bourn-Vita     Food     70         1          65
                 4        Soap  Hygiene     40         4         155
            Note that in the expression groceryDF['TotalPrice'] - 5, while groceryDF['TotalPrice'] is a series,
            5 is a scalar quantity. Broadcasting is feature of Python that makes the vector operands compatible. Thus, 5 is also
            expanded to a series of length len(groceryDF) to construct the series pd.Series([5]*len(groceryDF)) so
            that subtraction can be carried out smoothly. Suppose, the store wants to allow a further discount of five rupees on
            the purchase of each item. Below, we achieve this, while illustrating the concept of broadcasting.
             >>> # Construct a list of size len(groceryDF), each item in the list being 5
             >>> discountLst = [5]*len(groceryDF)
             >>> print('discount list:', discountLst)
             >>> # Construct a series from the discountLst
             >>> discountSeries = pd.Series(discountLst)
             >>> # Apply five rupees discount to total price
             >>> groceryDF['TotalPrice'] = groceryDF['TotalPrice'] - discountSeries
             >>> print(groceryDF.head())
                 discount list: [5, 5, 5, 5, 5, 5, 5, 5]
                       Product Category  Price  Quantity  TotalPrice
                 0       Bread     Food     20         2          30
                 1        Milk     Food     60         5         290
                 2     Biscuit     Food     20         2          30
                 3  Bourn-Vita     Food     70         1          60
                 4        Soap  Hygiene     40         4         150

            Thus, we can also modify the values in an existing column by assigning new values to it.
            Adding Rows to a DataFrame

            Suppose, we want to add two rows having the following information:

                       'Product'     'Category'   'Price'     'Quantity'
                 Row1: 'Jeans'       'Clothes'     400         2
                 Row2: 'Chocolate'   'Food'        50          4
            For this purpose, we perform the following steps:
            1.  Construct a dictionary having the above information:

            2.  Create a DataFrame using the dictionary in step 1.


                                                                             Data Handling using Pandas DataFrame  55
   64   65   66   67   68   69   70   71   72   73   74