Friday, August 27, 2021

Series Solutions

Series - Program & Solution

Contents:

  • Practice Programs with solution
  • Assignment Programs with solution

Practice Program

1.Write python code to create a Series object using the python sequence [4,6,8,10]. Assume that pandas is imported as alias name pd.

In [1]:
import pandas as pd
sr=pd.Series(data=[4,6,8,10])
print("The series object created is: ")
print(sr)
The series object created is: 
0     4
1     6
2     8
3    10
dtype: int64

2.Write code to create a Series object using the python sequence(11,21,31,41). Assume that pandas is imported as alias name pd

In [2]:
import pandas as pd
sr=pd.Series(data=(11,21,31,41))
print("The series object created is: ")
print(sr)
The series object created is: 
0    11
1    21
2    31
3    41
dtype: int64

3.Write a python program to create a Series object using a dictionary that stores the item name and its price of your grocery list.
maggie 10
salt 25
mustard oil 150
turmeric 25

In [1]:
import pandas as pd
gr={"maggie":10, "salt":25, "mustard oil":150, "turmeric":25}
sr=pd.Series(data=gr)
print(sr)
maggie          10
salt            25
mustard oil    150
turmeric        25
dtype: int64

4.Write a program to create a series object using an ndarray that has 10 number in the range of 20 to 25

In [2]:
import pandas as pd
import numpy as np

arr=np.linspace(20,25,10)
sr=pd.Series(data=arr)
print(sr)
0    20.000000
1    20.555556
2    21.111111
3    21.666667
4    22.222222
5    22.777778
6    23.333333
7    23.888889
8    24.444444
9    25.000000
dtype: float64
  1. Consider the series object listed below, Write python code to the amount section 'A' as 7500 and for section 'c' and 'd' as 6000. print the changed object.
    A 6700
    B 5000
    C 4800
    D 8700
In [6]:
import pandas as pd
dc={"A":6700,"B":5000,"C":4800,"D":8700}
sr=pd.Series(dc)
print("Original \n",sr)

sr[1]=7500
sr[2:]=6000

print("Modified \n",sr)
Original 
 A    6700
B    5000
C    4800
D    8700
dtype: int64
Modified 
 A    6700
B    7500
C    6000
D    6000
dtype: int64

6.A series object data consists of around 2500 rows of data. write a program to print the following details.

  1. First 100 rows of the data
  2. Last 5 rows of data
In [8]:
import pandas as pd

sr=pd.Series(range(100,250))
print(sr.head(100))
print(sr.tail(5))
0     100
1     101
2     102
3     103
4     104
     ... 
95    195
96    196
97    197
98    198
99    199
Length: 100, dtype: int64
145    245
146    246
147    247
148    248
149    249
dtype: int64

7.Number of students in class 11 and 12 in 2 streams ("Science","Humanities") are stored in a series object, write code to find total number of students in each stream combining both the class.Take any data you want in series object.

In [2]:
import pandas as pd
sr11=pd.Series(data={"science":20,"humanities":15})
sr12=pd.Series(data={"science":25,"humanities":35})

print("total number of students in each stream is:")
print(sr11+sr12)
total number of students in each stream is:
science       45
humanities    50
dtype: int64

8.srPop store the population details of four states in india and srIncome stores the total income reported in previous year in each city. Calculate income per capita for each of these cities.
population data:
delhi=10927986
mumbai=12691836
kolkata=46132392
chennai=4328956
Total income data:
delhi=75467963145698
mumbai=78423694528478
kolkata=4789657893214
chennai=9845637852496

In [4]:
import pandas as pd

srPop=pd.Series(data=[10927986,12691836,46132392,4328956], index=["delhi","mumbai","kolkata","chennai"])
srIncome=pd.Series(data=[75467963145698,78423694528478,4789657893214,9845637852496], index=["delhi","mumbai","kolkata","chennai"])

print("Percapita income in each city: ")
print(srIncome/srPop)
Percapita income in each city: 
delhi      6.905935e+06
mumbai     6.179066e+06
kolkata    1.038242e+05
chennai    2.274368e+06
dtype: float64

9.Series objects temp1, temp2, temp3, temp4 store the temperature of days of week1, week2, week3, week4 respectively. Write a script to
a. print average temperature per week
b. print average temperature of entire month

In [18]:
import pandas as pd

temp1=pd.Series(data={'mon':24,"tue":26,"wed":24,"thus":23,"fri":26,"sat":25,"sun":25})
temp2=pd.Series(data={'mon':25,"tue":26,"wed":25,"thus":24,"fri":25,"sat":26,"sun":23})
temp3=pd.Series(data={'mon':24,"tue":25,"wed":23,"thus":24,"fri":27,"sat":27,"sun":24})
temp4=pd.Series(data={'mon':22,"tue":23,"wed":26,"thus":25,"fri":26,"sat":25,"sun":25})

print("Average temperature per week: ")
print(pd.Series(data={"week1":temp1.mean(),"week2":temp2.mean(),"week3":temp3.mean(),"week4":temp4.mean()}))
print()
print()
print("Average temperature of entire month: ")
print((pd.Series(data=(temp1.mean(),temp2.mean(),temp3.mean(),temp4.mean())).mean()))
Average temperature per week: 
week1    24.714286
week2    24.857143
week3    24.857143
week4    24.571429
dtype: float64


Average temperature of entire month: 
24.75

10.Write a python 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 store sales of these 5 items in 1st month, s2 stores the sales of these 5 items in the 2nd month and so on, consider the following table of data where each row is a series object:

Series Object item1 item2 item3 item4 item5
s1 4578 658 4753 452 1256
s2 4568 6158 753 4252 156
s3 3578 6581 4713 4152 5256
s4 1578 258 753 4152 1856
s5 1278 6158 4153 432 156
s6 1278 6158 4153 432 156
s7 1278 6158 4153 432 156
s8 1278 6158 4153 432 156
s9 1278 6158 4153 432 156
s10 1278 6158 4153 432 156
s11 1278 6158 4153 432 156
s12 1278 6158 4153 432 156

The program should display the summary sales report like this:

  • Total yearly sales, item-wise(should display sum of items' sales over the month)
  • Maximum sales of items made: (name of items that was sold the maximum in whole year)
  • Maximum sales of individual items
    • Maximum sales of item 1 made: (month in which that items sold the maximum)
    • Maximum sales of item 2 made: (month in which that items sold the maximum)
    • Maximum sales of item 3 made: (month in which that items sold the maximum)
    • Maximum sales of item 4 made: (month in which that items sold the maximum)
    • Maximum sales of item 5 made: (month in which that items sold the maximum)
In [26]:
import pandas as pd
idnx=["item1","item2","item3","item4","item5"]
s1=pd.Series(data=[4578,658,4753,452,1256],index=idnx)
s2=pd.Series(data=[4568,6158,753,4252,156],index=idnx)
s3=pd.Series(data=[3578,6581,4713,4152,5256],index=idnx)
s4=pd.Series(data=[1578,258,753,4152,1856],index=idnx)
s5=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s6=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s7=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s8=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s9=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s10=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s11=pd.Series(data=[1278,6158,4153,432,156],index=idnx)
s12=pd.Series(data=[1278,6158,4153,432,156],index=idnx)

print("Yearly Sales Itemwise:")
print(pd.Series(s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12))
print()
print()
print("Items that was sold the maximum in whole year:")
print((pd.Series(s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12)).idxmax())
print()
print()
print("Maximum sales of individual items:")

def monNm(a):
    if a==0:
        return "January"
    elif a==1:
        return "February"
    elif a==2:
        return "March"
    elif a==3:
        return "April"
    elif a==4:
        return "May"
    elif a==5:
        return "June"
    elif a==6:
        return "July"
    elif a==7:
        return "August"
    elif a==8:
        return "September"
    elif a==9:
        return "october"
    elif a==10:
        return "November"
    elif a==11:
        return "December"
    
srtemp=pd.Series((s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12))
max=0
mnam=""
for items in srtemp.iteritems():
    if max<items[1]['item1']:
        max=items[1]['item1']
        mnam=monNm(items[0])
print("maximum of item1",mnam)
mnam=""
max=0

for items in srtemp.iteritems():
    if max<items[1]['item2']:
        max=items[1]['item2']
        mnam=monNm(items[0])
print("maximum of item2",mnam)
mnam=""
max=0

for items in srtemp.iteritems():
    if max<items[1]['item3']:
        max=items[1]['item3']
        mnam=monNm(items[0])
print("maximum of item3",mnam)
mnam=""
max=0

for items in srtemp.iteritems():
    if max<items[1]['item4']:
        max=items[1]['item4']
        mnam=monNm(items[0])
print("maximum of item4",mnam)
mnam=""
max=0

for items in srtemp.iteritems():
    if max<items[1]['item5']:
        max=items[1]['item5']
        mnam=monNm(items[0])
print("maximum of item5",mnam)
mnam=""
max=0
Yearly Sales Itemwise:
item1    24526
item2    62919
item3    44196
item4    16464
item5     9772
dtype: int64


Items that was sold the maximum in whole year:
item2


Maximum sales of individual items:
maximum of item1 January
maximum of item2 March
maximum of item3 January
maximum of item4 February
maximum of item5 March

11.Three series object store the marksof 10 students in three terms. Roll numbers of students from the index of these Series object. 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% Terms 2 + 50% Terms 3

In [3]:
import pandas as pd
roll=[1,2,3,4,5,6,7,8,9,10]
trm1=pd.Series(data=[452,356,489,476,463,432,395,398,452,436],index=roll)
trm2=pd.Series(data=[469,356,499,476,463,392,395,418,252,336],index=roll)
trm3=pd.Series(data=[472,358,489,496,333,332,495,354,352,498],index=roll)

finalMarks=pd.Series(trm1*0.25+trm2*0.25+trm3*0.5)
print("Final marks for each student from all the terms combined")
print()
print(finalMarks)
Final marks for each student from all the terms combined

1     466.25
2     357.00
3     491.50
4     486.00
5     398.00
6     372.00
7     445.00
8     381.00
9     352.00
10    442.00
dtype: float64
In [ ]:
 
Share: