Q. Given three dataframes namely Sales18, Sales19 and Sales20 storing quarter wise sales of items ‘Beverages', 'Milk', 'Snack items', 'Icecreams' and 'Bakery items'.
Write a script that prints the following:
(i) Maximum yearly sales item-wise.
(ii) Maximum Quarterly sale for an item across three years (e.g., Snack items' had maximum sales in Quarter 3 of year 2019).
(iii) Average sales Quarter wise for all items year wise.
(iv) Average sales Quarter wise for all items for all years.
Answer :-
import pandas as pd dict1={'Beverages':[1,82,13,30],'Milk':[11,22,44,87],'Snack items':[110,121,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[10,21,14,17]} dict2={'Beverages':[1,82,13,30],'Milk':[11,202,44,74],'Snack items':[109,281,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[101,21,14,17]} dict3={'Beverages':[1,282,813,130],'Milk':[11,22,44,54],'Snack items':[10,121,141,17]\ ,'Icecreams':[411,318,109,240],'Bakery items':[800,21,14,17]} lis=['Beverages','Milk','Snack items','Icecreams','Bakery items'] sales18=pd.DataFrame(dict1) sales19=pd.DataFrame(dict2) sales20=pd.DataFrame(dict3) #part 1 Maximum yearly sales item-wise dicc={} maximum=0 for i in lis: maximum+=sales18[i].sum() maximum+=sales19[i].sum() maximum+=sales20[i].sum() dicc[i]=maximum print('Maximum yearly sales item-wise') print() for i in lis: maxkey=max(dicc,key=dicc.get) print(maxkey,'\t',dicc[maxkey]) del dicc[maxkey]
import pandas as pd dict1={'Beverages':[1,82,13,30],'Milk':[11,22,44,87],'Snack items':[110,121,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[10,21,14,17]} dict2={'Beverages':[1,82,13,30],'Milk':[11,202,44,74],'Snack items':[109,281,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[101,21,14,17]} dict3={'Beverages':[1,282,813,130],'Milk':[11,22,44,54],'Snack items':[10,121,141,17]\ ,'Icecreams':[411,318,109,240],'Bakery items':[800,21,14,17]} items=['Beverages','Milk','Snack items','Icecreams','Bakery items'] sales18=pd.DataFrame(dict1) sales19=pd.DataFrame(dict2) sales20=pd.DataFrame(dict3) #part 2 Maximum Quarterly sale for an item across three years dic={1:sales18,2:sales19,3:sales20} maximum=0 item_n='' quarter=0 for i in lis: for j in dic: if maximum < dic[j][i].sum(): maximum=dic[j][i].sum() quarter=j item_n=i print(f' {item_n} had maximum sales in Quarter {quarter} ')
import pandas as pd dict1={'Beverages':[1,82,13,30],'Milk':[11,22,44,87],'Snack items':[110,121,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[10,21,14,17]} dict2={'Beverages':[1,82,13,30],'Milk':[11,202,44,74],'Snack items':[109,281,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[101,21,14,17]} dict3={'Beverages':[1,282,813,130],'Milk':[11,22,44,54],'Snack items':[10,121,141,17]\ ,'Icecreams':[411,318,109,240],'Bakery items':[800,21,14,17]} items=['Beverages','Milk','Snack items','Icecreams','Bakery items'] sales18=pd.DataFrame(dict1) sales19=pd.DataFrame(dict2) sales20=pd.DataFrame(dict3) #part 3 average sales Quarter wise for all items year wise. dic={1:sales18,2:sales19,3:sales20} print('average sales Quarter wise for all items') print() for i in items: for j in dic: print(f'average sales of {i} in quater {j} :- ',end=' ') print(dic[j][i].sum()/4) print()
import pandas as pd dict1={'Beverages':[1,82,13,30],'Milk':[11,22,44,87],'Snack items':[110,121,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[10,21,14,17]} dict2={'Beverages':[1,82,13,30],'Milk':[11,202,44,74],'Snack items':[109,281,14,17]\ ,'Icecreams':[11,18,19,20],'Bakery items':[101,21,14,17]} dict3={'Beverages':[1,282,813,130],'Milk':[11,22,44,54],'Snack items':[10,121,141,17]\ ,'Icecreams':[411,318,109,240],'Bakery items':[800,21,14,17]} items=['Beverages','Milk','Snack items','Icecreams','Bakery items'] sales18=pd.DataFrame(dict1) sales19=pd.DataFrame(dict2) sales20=pd.DataFrame(dict3) #part 4 similar as 3 dic={1:sales18,2:sales19,3:sales20} print('average sales Quarter wise for all items') print() for i in items: for j in dic: print(f'average sales of {i} in quater {j} :- ',end=' ') print(dic[j][i].sum()/4) print()
Output :-
(i)
Maximum yearly sales item-wise
Bakery items 5357
Icecreams 4290
Snack items 3076
Milk 2104
Beverages 1478
>>>
(ii)
Beverages had maximum sales in Quarter 3
>>>
(iii)
average sales Quarter wise for all items
average sales of Beverages in quater 1 :- 31.5
average sales of Beverages in quater 2 :- 31.5
average sales of Beverages in quater 3 :- 306.5
average sales of Milk in quater 1 :- 41.0
average sales of Milk in quater 2 :- 82.75
average sales of Milk in quater 3 :- 32.75
average sales of Snack items in quater 1 :- 65.5
average sales of Snack items in quater 2 :- 105.25
average sales of Snack items in quater 3 :- 72.25
average sales of Icecreams in quater 1 :- 17.0
average sales of Icecreams in quater 2 :- 17.0
average sales of Icecreams in quater 3 :- 269.5
average sales of Bakery items in quater 1 :- 15.5
average sales of Bakery items in quater 2 :- 38.25
average sales of Bakery items in quater 3 :- 213.0
>>>
(iv)
average sales Quarter wise for all items
average sales of Beverages in quater 1 :- 31.5
average sales of Beverages in quater 2 :- 31.5
average sales of Beverages in quater 3 :- 306.5
average sales of Milk in quater 1 :- 41.0
average sales of Milk in quater 2 :- 82.75
average sales of Milk in quater 3 :- 32.75
average sales of Snack items in quater 1 :- 65.5
average sales of Snack items in quater 2 :- 105.25
average sales of Snack items in quater 3 :- 72.25
average sales of Icecreams in quater 1 :- 17.0
average sales of Icecreams in quater 2 :- 17.0
average sales of Icecreams in quater 3 :- 269.5
average sales of Bakery items in quater 1 :- 15.5
average sales of Bakery items in quater 2 :- 38.25
average sales of Bakery items in quater 3 :- 213.0
>>>
Ans plz
ReplyDeleteOk.
DeleteAs soon as possible
ReplyDeletePlease check again. : )
DeleteLengthy Solutions
ReplyDeleteSolution of dataframe are so long. So, do not try to learn program always try to understand the program.
DeleteWhy 3 and 4 have the same output?
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )