Informatics Practices
Write a program that stores the sales of 5 fast moving items of a store for each month in 12 Series objects, i.e., S1 Series object stores sales of these 5 items in 1st month, S2 stores sales of these 5 items in 2nd month, and so on.
The program should display the summary sales report like this :
Total Yearly Sales, item-wise (should display sum of items' sales over the months)
Maximum sales of item made : <name of item that was sold the maximum in whole year>
Maximum sales for individual items
Maximum sales of item 1 made : <month in which that item sold the maximum>
Maximum sales of item 2 made : <month in which that item sold the maximum>
Maximum sales of item 3 made : <month in which that item sold the maximum>
Maximum sales of item 4 made : <month in which that item sold the maximum>
Maximum sales of item 5 made : <month in which that item sold the maximum>
Python Pandas
1 Like
Answer
import pandas as pd
sales_data = {
'Month_1': pd.Series([300, 250, 200, 150, 350], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_2': pd.Series([380, 210, 220, 180, 320], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_3': pd.Series([320, 270, 230, 200, 380], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_4': pd.Series([310, 260, 210, 190, 360], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_5': pd.Series([290, 240, 220, 170, 340], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_6': pd.Series([300, 250, 400, 160, 350], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_7': pd.Series([310, 260, 230, 180, 370], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_8': pd.Series([320, 270, 240, 190, 380], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_9': pd.Series([330, 280, 250, 200, 400], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_10': pd.Series([340, 290, 260, 510, 420], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_11': pd.Series([350, 300, 270, 220, 440], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_12': pd.Series([360, 390, 280, 230, 260], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5'])
}
sales_df = pd.DataFrame(sales_data)
print("Total Yearly Sales, item-wise:")
total_sales = sales_df.sum()
print(total_sales)
t = sales_df.sum(axis=1)
max_sales_item = t.idxmax()
print("\nMaximum sales of item made: ", max_sales_item)
print("\nMaximum sales for individual items:")
for item_num in range(1, 6):
max_sales_month = None
max_sales_value = 0
for month in sales_df.columns:
if sales_df[month][f'Item_{item_num}'] > max_sales_value:
max_sales_value = sales_df[month][f'Item_{item_num}']
max_sales_month = month
print("Maximum sales of item", item_num, "made: ", max_sales_month)Output
Total Yearly Sales, item-wise:
Month_1 1250
Month_2 1310
Month_3 1400
Month_4 1330
Month_5 1260
Month_6 1460
Month_7 1350
Month_8 1400
Month_9 1460
Month_10 1820
Month_11 1580
Month_12 1520
dtype: int64
Maximum sales of item made: Item_5
Maximum sales for individual items:
Maximum sales of item 1 made: Month_2
Maximum sales of item 2 made: Month_12
Maximum sales of item 3 made: Month_6
Maximum sales of item 4 made: Month_10
Maximum sales of item 5 made: Month_11
Answered By
1 Like
Related Questions
Consider the following DataFrame df and answer any four questions from (i)-(v):
rollno name UT1 UT2 UT3 UT4 1 Prerna Singh 24 24 20 22 2 Manish Arora 18 17 19 22 3 Tanish Goel 20 22 18 24 4 Falguni Jain 22 20 24 20 5 Kanika Bhatnagar 15 20 18 22 6 Ramandeep Kaur 20 15 22 24 Which of the following command will display the column labels of the DataFrame ?
(a) print(df.columns())
(b) print(df.column())
(c) print(df.column)
(d) print(df.columns)
Consider the following DataFrame df and answer any four questions from (i)-(v):
rollno name UT1 UT2 UT3 UT4 1 Prerna Singh 24 24 20 22 2 Manish Arora 18 17 19 22 3 Tanish Goel 20 22 18 24 4 Falguni Jain 22 20 24 20 5 Kanika Bhatnagar 15 20 18 22 6 Ramandeep Kaur 20 15 22 24 Ms. Sharma, the class teacher wants to add a new column, the scores of Grade with the values, 'A', 'B', 'A', 'A', 'B', 'A' , to the DataFrame.
Help her choose the command to do so :
(a) df.column = ['A', 'B', 'A', 'A', 'B', 'A']
(b) df['Grade'] = ['A', 'B', 'A', 'A', 'B', 'A']
(c) df.loc['Grade'] = ['A', 'B', 'A', 'A', 'B', 'A']
(d) Both (b) and (c) are correct
Three Series objects store the marks of 10 students in three terms. Roll numbers of students form the index of these Series objects. The Three Series objects have the same indexes.
Calculate the total weighted marks obtained by students as per following formula :
Final marks = 25% Term 1 + 25% Term 2 + 50% Term 3Store the Final marks of students in another Series object.
Write code to print all the information about a Series object.