SERIES
Name: rayyan abdul rahman
Creating a series using dictionary
Aim: to write a python program to create a series to store 5 students percentage using dictionary and
print all the elements that are above 75 pecenetage
Source code:
import pandas as pd
D={'arun':65,'bala':91,'charan':74,'dinesh':80,'usha':85}
S=pd.Series(D)
print(S>75)
print(S[S>75])
Output:
arun False
bala True
charan False
dinesh True
usha True
dtype: bool
bala 91
dinesh 80
usha 85
dtype: int64
Result:
thus, above python program is executed successfully and the output is verified
Creating a series using scalar values
Aim:
To write a python program to create a series object that stores the initial budget allocated (50000
rupees each) for the four quarters of the year: Qtr 1,Qtr 2,Qtr 3,Qtr 4
Source code:
import pandas as pd
budget=pd.Series(50000,index=['qtr1','qtr2','qtr3','qtr4'])
print(budget)
Output:
qtr1 50000
qtr2 50000
qtr3 50000
qtr4 50000
dtype: int64
Result:
thus, above python program is executed successfully and the output is verified
Creating a series using numpy array
Aim:
To write a python program to create a series object that stores the section names [‘A’,’B’,’C’] as the
index and contribution made by them respectively (6700,5500,nil) as values.
Source code:
import pandas as pd
import numpy as np
section=['a','b','c']
contribution=np.array([6700,5500,np.NaN])
S=pd.Series(data=contribution,index=section)
print(S)
Output:
a 6700.0
b 5500.0
c NaN
dtype: float64
Result:
thus, above python program is executed successfully and the output is verified
Performing mathematical calculations
Aim:
To create a program in python to perform following mathematical operations on two series objects:
(i)addition
(ii)subtraction
(iii)multiplication
(iv)division
Source code:
import pandas as pd
s1=pd.Series([10,20,30],index=['a','b','c'])
s2=pd.Series([2,5],index=['a','b'])
print('the addition of two series object is:')
print(s1+s2)
print('the subtraction of two series object is:')
print(s1-s2)
print('the mulitiplication of two series object is:')
print(s1*s2)
print('the division of two series object is:')
print(s1/s2)
Output:
the addition of two series object is:
a 12.0
b 25.0
c NaN
dtype: float64
the subtraction of two series object is:
a 8.0
b 15.0
c NaN
dtype: float64
the mulitiplication of two series object is:
a 20.0
b 100.0
c NaN
dtype: float64
the division of two series object is:
a 5.0
b 4.0
c NaN
dtype: float64
Result:thus, above python program is executed successfully and the output is verified
Displaying attributes of a series
Aim:
to write a python program to create a series using list and display the following attributes of the series:
(i) index
(ii) dtype
(iii) size
(iv) shape
(v) hasnans
Source code:
import pandas as pd
L=[10,45,67,3,43]
S=pd.Series(L,index=['a','b','c','d','e'])
print('the index of the series is:',S.index)
print('the dtype of the series is:',S.dtype)
print('the size of the series is:',S.size)
print('the NaN of the series is:',S.hasnans)
Output:
the index of the series is: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
the dtype of the series is: int64
the size of the series is: 5
the NaN of the series is: False
Result:
thus, above python program is executed successfully and the output is verified
HEAD AND TAIL
Aim: a program to enter the name and mark of 10 students and display first five and last three
Source code:
import pandas as pd
d={'rayyan':95,'vignesh':99,'sreerang':42,'meshaal':90,'neev':77,'nabeel':84,'alex':87,'adhi':67,'azhar':76,'
ayaan':77}
S=pd.Series(d)
print(S.head())
print(S.tail(3))
Output:
rayyan 95
vignesh 99
sreerang 42
meshaal 90
neev 77
dtype: int64
adhi 67
azhar 76
ayaan 77
dtype: int64
Result:
thus, above python program is executed successfully and the output is verified
AIM: Program to create series 34,29,65,57,42
SOURCE CODE:
import pandas as pd
l=[34,29,65,57,42]
s=pd.Series(l)
print(l)# raised to the power 2 of value
print(s**2)
#add 1000
print(s+1000)
#subrtact 500
print(s-500)
#multiply by 2
print(s*2)
#divide by 20
print(s/20)
#display all values greater than 20
print(s[s>50])
output:
0 34
1 29
2 65
3 57
4 42
dtype: int64
0 1156
1 841
2 4225
3 3249
4 1764
dtype: int64
0 1034
1 1029
2 1065
3 1057
4 1042
dtype: int64
0 -466
1 -471
2 -435
3 -443
4 -458
dtype: int64
0 68
1 58
2 130
3 114
4 84
dtype: int64
0 1.70
1 1.45
2 3.25
Aim: to write a python program to create a pandas dataframe called df for the following table using dictionary of list and perform the
following operations
i)insert a new column ‘bags’ with values as [5891,8628,9785,4475]
ii)delete the row details of MP from DataFrame DF
iii)delete the column bags
Source code:
import pandas as pd
D={'toys':[7916,8508,7226,7616],'uniform':[610,508,611,457],'shoes':[8810,6798,9611,6457]}
DF=pd.DataFrame(D,index=['AF','OD','M.F','U.F'])
DF['bags']=[5891,8628,9785,4475]
print('after adding a new column:')
print(DF)
print('\n')
del DF['bags']
print(DF)
Output:
after adding a new column:
after adding a new column:
toys uniform shoes bags
AF 7916 610 8810 5891
OD 8508 508 6798 8628
M.F 7226 611 9611 9785
U.F 7616 457 6457 4475
toys uniform shoes
AF 7916 610 8810
OD 8508 508 6798
M.F 7226 611 9611
U.F 7616 457 6457
Aim: write a python program to create a panda’s dataframe called DF for the following table using dictionary of list
and display the details of students whose percentage is more than 85.
Source code:
import pandas as pd
D={'stu_name':['anu','arun','bala','charan','mano'],'degree':
['MBA','MCA','M.E','M.SC','MCA'],'percentage':[90,85,91,76,84]}
DF=pd.DataFrame(D,index=['S1','S2','S3','S4','S5'])
print(DF['percentage']>85)
print(DF.loc[DF['percentage']>85])
Output:
S1 True
S2 False
S3 True
S4 False
S5 False
Name: percentage, dtype: bool
stu_name degree percentage
S1 anu MBA 90
S3 bala M.E 91
Aim: write a python program to create a dataframe using a dictionary and display the following attributes
Code:
import pandas as pd
d={'stu_name':['anu','bala','charan','mano'],'degree':['mba','mca','m.e','m.sc'],'percent':[90,85,91,76]}
df=pd.DataFrame(d,index=['stu1','stu2','stu3','stu4'])
print("\nThe index of the dataframe is:",df.index)
print('\nthe columns of the dataframe is:',df.columns)
print('\nthe axes of the dataframe is',df.axes)
print('\nthe datatypes in the dataframe is',df.dtypes)
print('\nthe dimension of of the dataframe is',df.ndim)
print('\nthe shape of the dataframe is',df.shape)
print('\nthe transpose of the dataframe is',df.T)
output:
The index of the dataframe is: Index(['stu1', 'stu2', 'stu3', 'stu4'], dtype='object')
the columns of the dataframe is: Index(['stu_name', 'degree', 'percent'], dtype='object')
the axes of the dataframe is [Index(['stu1', 'stu2', 'stu3', 'stu4'], dtype='object'), Index(['stu_name', 'degree', 'percent'], dtype='object')]
the datatypes in the dataframe is stu_name object
degree object
percent int64
dtype: object
the dimension of of the dataframe is 2
the shape of the dataframe is (4, 3)
the transpose of the dataframe is stu1 stu2 stu3 stu4
stu_name anu bala charan mano
degree mba mca m.e m.sc
percent 90 85 91 76
result: the dataframe at
Aim: to create pandas dataframe called df using dictionary of list and display details of students whos percentage is more than 85
Stu_name degree percentage
S1 Anu mba 90
S2 arun Mca 85
S3 bala m.e 91
S4 charan m.sc 76
S5 mano Mca 84
Code:
import pandas as pd
D={'stu_name':['anu','arun','bala','charan','mano'],'degree':['mba','mca','m.e','m.sc','mca'],'percentage':[90,85,91,76,84]}
DF=pd.DataFrame(D,index=['s1','s2','s3','s4','s5'])
print(DF.loc[DF['percentage']>85])
output:
stu_name degree percentage
s1 anu mba 90
s3 bala m.e 91
result:
the dataframe has been printed succusfully
Aim: create a pandas dataframe called student and demonstrate iterrows and iteritems
Code:
import pandas as pd
D={'stu_name':['anu','arun','bala','charan','mano'],'degree':['mba','mca','m.e','m.sc','mca'],'percentage':[90,85,91,76,84]}
DF=pd.DataFrame(D,index=['s1','s2','s3','s4','s5'])
for (row,values) in DF.iterrows():
print(row)
print(values)
print()
for (col,values) in DF.iteritems():
print(col)
print(values)
print()
output:
s1
stu_name anu
degree mba
percentage 90
Name: s1, dtype: object
s2
stu_name arun
degree mca
percentage 85
Name: s2, dtype: object
s3
stu_name bala
degree m.e
percentage 91
Name: s3, dtype: object
s4
stu_name charan
degree m.sc
percentage 76
Name: s4, dtype: object
s5
stu_name mano
degree mca
percentage 84
Name: s5, dtype: object
Q. given is a table of birds and their respective population. Write python code to create a bar
graph
BIRDS POPULATION
PEACOCK 2600
PARROT 3000
MONAL 1000
FLYCATCHER 5000
CROW 1200
CODE:
import matplotlib.pyplot as plt
x=['peacock','parrot','monal','flycatcher','crow']
y=[2600,3000,1000,5000,1200]
plt.bar(x,y,color='g')
plt.xlabel='BIRDS'
plt.ylabel='POPULATION'
plt.show()
OUTPUT:
RESULT:
The program has worked successfully
AIM: program to draw a histogram where heights of the bar are [10,1,0,33,6,8]
CODE:
import matplotlib.pyplot as plt
bin=[0,10,20,30,40,50,60]
x=[1,11,21,31,41,51]
weight=[10,1,0,33,6,8]
plt.hist(x,bins=bin,weights=weight,color='r',edgecolor='k')
plt.xlabel='value'
plt.ylabel='frequency'
plt.title='frequency chart'
plt.show()
OUTPUT:
AIM: to write a python program to plot a line chart to depict the changing weekly onion and brinjal prices for four
weks. Also give appropriate axes labels, title and keep marker style as diamond and marker edge as red for onion.
SOURCE CODE:
import matplotlib.pyplot as plt
weeks=[1,2,3,4]
onion=[45,25,32,80]
brinjal=[16,18,45,50]
plt.title('price analysis')
plt.xlabel('weeks')
plt.ylabel('prices')
plt.plot(weeks,onion,marker='D',markersize=15,markeredgecolor='r')
plt.plot(weeks,brinjal)
plt.show()
OUTPUT;