Programming Problems (Type – C)
6. 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 : <rzonth in which that item sold the maximum>
Ans:
import pandas as pd
import numpy as np
items = [‘SOAP’, ‘TEA’, ‘RICE’, ‘DAL’, ‘TOYS’]
jan = pd.Series([25,35,12,65,98], index=items)
feb = pd.Series([36,48,95,12,18], index=items)
mar = pd.Series([12,50,25,35,58], index=items)
apr = pd.Series([45,36,45,98,12], index=items)
may = pd.Series([68,36,45,98,12], index=items)
jun = pd.Series([140,36,45,98,12], index=items)
jul = pd.Series([154,99,87,78,47], index=items)
aug = pd.Series([75,36,45,98,65], index=items)
sep = pd.Series([60,36,45,98,105], index=items)
octo =pd.Series([70,68,54,15,25], index=items)
nov = pd.Series([80,56,80,8,62], index=items)
dec = pd.Series([105,15,40,98,22], index=items)
df = pd.DataFrame({‘JAN’:jan, ‘FEB’:feb, ‘MAR’:mar,
‘APR’:apr, ‘MAY’:may, ‘JUN’:jun,
‘JUL’:jul, ‘AUG’:aug, ‘SEP’:sep,
‘OCT’:octo,’NOV’:nov,’DEC’:dec},
index=items)
print(“-“*30)
print(“Total Yearly Sales Item Wise”)
maxitem=[0,”]
for item in items:
total = df.loc[item].sum()
if total > maxitem[0]:
maxitem[0]=total
maxitem[1]=item
print(item, “==>”, total)
print(“-“*30)
print(“-“*30)
print(“Maximum sales of item made : “, maxitem[1] ,”is”, maxitem[0])
print(“-“*30)
print(“-“*30)
print(“Name of Month (Maximum Sale) Item Wise”)
print(“-“*30)
t = 0
for i in items:
monthname = df.columns[(df == df.loc[i].max()).iloc[t]][0]
print(i,’sold maximun’, df.loc[i].max(), end=’ ‘)
print(‘in month of’, monthname)
t = t +1
print(“-“*30)
7. 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 3
Store the Final marks of students in another Series object.
Ans:
import pandas as pd
import numpy as np
term1=[20,15,25,22,24,21,12,20,30,28]
term2=[27,17,23,19,28,29,30,16,25,19]
term3=[25,15,22,26,26,27,30,20,28,28]
s1= pd.Series(term1, index=range(1,11))
s2= pd.Series(term2, index=range(1,11))
s3= pd.Series(term3, index=range(1,11))
finalMarks = 0.25 * s1 + 0.25 * s2 + 0.5 * s3
print(finalMarks)
8. Write code to print all the information about a Series object.
Ans:
import pandas as pd
import numpy as np
days = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’]
tempArr = np.random.randint(10,30,7)
series = pd.Series(tempArr, index=days)
print(“Series Values : “, series.values)
print(“Series Index : “, series.index)
print(“Series Size : “, series.size)
print(“Series Shape : “, series.shape)
print(“Series Empty : “, series.empty)
print(“Series has NaN :”, series.hasnans)
print(“Series Dimension : “, series.ndim)
print(“Series Data Types: “, series.dtypes)
print(“Series nbytes : “, series.nbytes)
9. Write a program to create three different Series objects from the three columns of a DataFrame df.
Ans:
import pandas as pd
import numpy as np
dic = {‘Maths’:[25,35,69,78],
‘Science’: [35,45,25,90],
‘SSt’: [65,25,85,35]}
dfScore = pd.DataFrame(dic)
print(“=”*30)
print(“DataFrame is”)
print(“-“*20)
print(dfScore)
serMath = dfScore[‘Maths’]
serSc = dfScore[‘Science’]
serSSt = dfScore[‘SSt’]
print(“=”*30)
print(“Series 1 : Math”)
print(“-“*20)
print(serMath)
print(“=”*30)
print(“Series 2 : Science”)
print(“-“*20)
print(serSc)
print(“=”*30)
print(“Series 3 : SSt”)
print(“-“*20)
print(serSSt)
print(“=”*30)