Page 322 - Informatics_Practices_Fliipbook_Class12
P. 322

(viii)  Retrieve the number of occurrences for each unique value in the Mode column.
             (ix)   Add  a  new  column  Energy  Efficiency  (kWh/km)  to  the  DataFrame  to  be  calculated  as  1  /  Fuel
                  Efficiency (km/l).

             (x)  Concatenate the DataFrame transportDF with a new DataFrame df2 in a row-wise manner.
             (xi)  Write the contents of the DataFrame transportDF to a CSV file named transport_details.csv.

             (xii)  Group the DataFrame by Mode and calculate the average values for each group.
             (xiii)  Drop the Fuel Efficiency (km/l) column from the DataFrame.
             (xiv)  Rename the column Speed (km/h) to Speed in km per hour.

        Ans. (i)   transportDF[['Mode', 'Capacity']]
             (ii)  transportDF[transportDF['Speed (km/h)'] > 50]

             (iii)  transportDF[transportDF['Fuel Efficiency (km/l)'] == 0]

             (iv)  transportDF.iloc[1:4]

             (v)  transportDF.index = ['A', 'B', 'C', 'D', 'E']
             (vi)  transportDF.describe()

             (vii)  transportDF['Speed (km/h)'].max()

             (viii)  transportDF['Mode'].value_counts()

             (ix)   transportDF['Energy Efficiency (kWh/km)'] = 1 / transportDF['Fuel Efficiency (km/l)']

             (x)  pd.concat([transportDF, df2], ignore_index=True)
             (xi)  transportDF.to_csv('transport_details.csv', index=False)

             (xii)  transportDF.groupby('Mode').mean()

             (xiii)  transportDF.drop(columns=['Fuel Efficiency (km/l)'], inplace=True)

             (xiv)   transportDF.rename(columns={'Speed (km/h)': 'Speed in km per hour'},
                  inplace=True)

         Program 11:  Consider  the  following  dataset  representing  the  monthly  tax  revenue  collected  by  a  government
         department for a year (in lakhs): [500, 600, 550, 700, 800, 750, 900, 850, 950, 1000, 950, 1100]. Write a Python
         program using Matplotlib to create a line plot to visualize the tax revenue trends. Provide appropriate labels for the
         axis  and  title  for  the  Figure.  The  Figure  should  comprise  gridlines  and  should  be  saved  as  "taxRevenue.png".

        Ans. import matplotlib.pyplot as plt

             taxRevenue = [500, 600, 550, 700, 800, 750, 900, 850, 950, 1000, 950, 1100]

              months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
             'Nov', 'Dec']

             plt.figure(figsize=(10, 6))  # Set the figure size

              plt.plot(months, taxRevenue, marker='o', linestyle='-', color='b', label='Tax
             Revenue (in lakhs)')

             plt.xlabel('Months')



          308  Touchpad Informatics Practices-XII
   317   318   319   320   321   322   323   324   325   326   327