Page 31 - Informatics_Practices_Fliipbook_Class12
P. 31

Day6    3800
                 Day7    4200
                 dtype: int64
            Example 3: Computing total revenue
            Let's consider two series, price and quantity, representing the price and quantity of five different products sold in past
            one hour. We want to calculate the total revenue generated by selling these products. We can do this by multiplying
            the price and quantity series as shown below:
              01 import pandas as pd
              02 products = ['Product1', 'Product2', 'Product3', 'Product4', 'Product5']
              03 price = pd.Series([10, 20, 30, 40, 35], index = products)
              04 quantity = pd.Series([100, 200, 300, 400, 300], index = products)
              05 totalRevenue = price * quantity
              06 print("Total Revenue: ")
              07 print(totalRevenue)
              08 print("Sum Total: ", totalRevenue.sum())
            output:
                 Total Revenue:
                 Product1     1000
                 Product2     4000
                 Product3     9000
                 Product4    16000
                 Product5    10500
                 dtype: int64
                 Sum Total:  40500

            Example 4: Comparing daily sales of two stores
            Suppose we have two Series that contain the daily sales of two stores for the last week. We want to compare the daily
            sales of the two stores to determine which store had higher sales on each day. We can perform this operation using
            the ">" operator as follows:
             >>> sales1 = [1000, 1200, 1400, 1600, 1800, 2000, 2000]
             >>> sales2 = [8000, 1000, 1200, 2400, 1600, 1800, 2200]
             >>> days = ['Day1', 'Day2', 'Day3', 'Day4', 'Day5', 'Day6', 'Day7']
             >>> store1Sales = pd.Series(sales1, index = days)
             >>> store2Sales = pd.Series(sales2, index = days)
             >>> higherSales = store1Sales > store2Sales
             >>> print(higherSales)
                 Day1    False
                 Day2     True
                 Day3     True
                 Day4    False
                 Day5     True
                 Day6     True
                 Day7    False
                 dtype: bool
             >>> higherSales = higherSales.replace({False:'Store2', True:'Store1'})
             >>> print(higherSales)
                 Day1    Store2
                 Day2    Store1
                 Day3    Store1
                 Day4    Store2
                 Day5    Store1
                 Day6    Store1
                 Day7    Store2
                 dtype: object



                                                                                      Data Handling using Pandas  17
   26   27   28   29   30   31   32   33   34   35   36