G Pandey Practical
G Pandey Practical
PRACTICAL FILE
Informatics Practices
Submitted By
Name: Cadet Gaurav Pandey
Roll No. : 3100/S
Index
S.No. List of Experiments
01. Experiment No. 02
02. Experiment No. 03
03. Experiment No. 04
04. Experiment No. 05
05. Experiment No. 06
06. Experiment No. 07
07. Experiment No. 08
08. Experiment No. 09
09. Experiment No. 10
10. Experiment No. 11
11. Experiment No. 12
12. Experiment No. 13
13. Experiment No. 14
14. Experiment No. 15
15. Experiment No. 16
16. Experiment No. 17
17. Experiment No. 18
18. Experiment No. 19
1
Experiment No. 01:
Write a Program to create a DataFrame from a 2D array as shown
below:
125 123 124
180 140 205
110 216 217
Program:
import pandas as pd
import numpy as np
array1=np.array([[125,123,124],[180,140,205],[110,216,217]])
dataframe1=pd.DataFrame(array1)
print(dataframe1)
Input:
Output:
2
Experiment No. 02:
XYZ is a list having three lists inside it. It contains summarized data of
three different trials conducted by company A. Create a bar chart that
plots these three sublists of ABC in a single chart. Keep the width of
each bar as 0.25.
XYZ = [[5, 25, 45, 20], [4, 23, 49, 17], [6, 22, 47, 19]]
Program:
import numpy as np
import matplotlib.pyplot as plt
Values = [[5, 25, 45, 20], [4, 23, 49, 17], [6, 22, 47, 19]]
W=np.arange(4)
plt.bar(W + 0.00 , Values[0], color='r', width = 0.25)
plt.bar(W + 0.25 , Values[1], color='b', width = 0.25)
plt.bar(W + 0.50 , Values[2], color='g', width = 0.25)
plt.show()
Input:
3
Output:
4
Experiment No. 03:
Write a program to iterate over a dataframe containing names and
marks which then calculates grades as per marks(as per guiding
below) and adds them to the grade column.
Marks>= 95 grade A+ ; Marks 50-60 grade C;
Marks 70-90 grade A ; Marks 40-50 grade D;
Marks 60-70 grade B ; Marks <40 grade F;
Program:
import pandas as pd
import numpy as np
names=pd.Series(['Rathore','Chaudhry','Hari','Omu'])
marks=pd.Series([85,77.5,87.5,62])
student={'Name': names,'Marks':marks}
Df1=pd.DataFrame(student,columns=['Name','Marks'])
Df1['Grade']=np.NaN
print('Initial value in DataFrame')
print(Df1)
for(col,colSeries) in Df1.iteritems():
length=len(colSeries)
if col == 'Marks':
lstmks=[]
for row in range(length):
mks=colSeries[row]
if mks>=90:
lstmks.append('A+')
elif mks>=70:
lstmks.append('A')
elif mks>=60:
lstmks.append('B')
elif mks>=50:
5
lstmks.append('C')
elif mks>=40:
lstmks.append('D')
else :
lstmks.append('F')
Df1['Grade'] = lstmks
print('DataFrame after calculating grade')
print(Df1)
Input:
6
Output:
7
Experiment No. 04:
Given a series fibn that contains reversed Fibonacci numbers with
Fibonacci numbers as shown below:
[0, -1, -1, -2, -3, -5, -8, -13, -21, -34, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Write a program to plot fibn with following specifications:
(a) The line color should be magenta.
(b) The marker edge color should be black with size 5.
(c) Grid should be displayed.
Program:
import matplotlib.pyplot as plt
fibn = [0,-1,-1,-2,-3,-5,-8,-13,-21,-34,0,1,1,2,3,5,8,13,21,34]
plt.plot(range(-10,10),fibn,'mo', markersize =5,markeredgecolor = 'r',
linestyle = 'solid')
plt.grid(True)
plt.show()
Input:
8
Output:
9
Experiment No. 05:
Write a program to plot a scatter graph taking three random
distributions in A, B, C (all three with shape as (225), having randomly
generated integers and plotted against each other with varying sizes.
Program:
import matplotlib.pyplot as plt
import numpy as np
A = np.random.randint(1,50,size=(225,))
B = np.random.randint(1,50,size=(225,))
C = np.random.randint(1,50,size=(225,))
plt.scatter(A,B,color ='g')
plt.scatter(A,C,color ='b')
plt.xlabel('A values')
plt.ylabel('B values')
plt.show()
Input:
10
Output:
11
Program:
import pandas as pd
import numpy as np
Section =['A','B','C','D','E',]
contrib = np.array( [6700,5600,5000,5200,np.NaN] )
gift= pd.Series(contrib*2 , index=Section, dtype='float32')
print(gift)
Input:
Output:
12
Program:
import numpy as np
import matplotlib.pyplot as plt
A = np.arange(1,20,1.25)
B = np.log(A)
plt.plot(A , B , 'go')
plt.xlabel('Random Values')
plt.ylabel('Logarithm Values')
plt.show()
Input:
13
Output:
14
Experiment No. 08:
Consider the saleDf shown below
Target Sales
ZoneX 56000 58000
ZoneY 70000 68000
ZoneW 75000 78000
ZoneP 60000 61000
Program:
import pandas as pd
ZoneA = {'Target':56000,'Sales':58000}
ZoneB = {'Target':70000,'Sales':68000}
ZoneC = {'Target':75000,'Sales':78000}
ZoneD = {'Target':60000,'Sales':61000}
sales = [ZoneA,ZoneB,ZoneC,ZoneD]
saleDf = pd.DataFrame(sales,index = ['ZoneA','ZoneB','ZoneC','ZoneD'])
print('The DataFrame saleDf:')
print(saleDf)
print('----------------------------------')
print('After Renaming: ')
saleDf.rename(index={'ZoneC':'Central', 'ZoneD':'Dakshin'},
columns = {'Target':'Targeted', 'Sales':'Achieved'},inplace=True)
print(saleDf)
15
Input:
Output:
16
Program:
import numpy as np
import pandas as pd
a=np.array([[74.25,56.03,59.30,69.00,89.65],
[76.06,68.71,72.07,78.47,79.65],
[69.50,62.89,77.65,65.53,80.75],
[72.55,56.42,66.46,76.85,85.08]])
srh=pd.DataFrame(a,columns=['Day1','Day2','Day3','Day4','Day5'])
print(srh)
17
Input:
Output:
18
19
Experiment No. 10:
Write a Pandas program to convert a dictionary to a Pandas series.
Sample dictionary: d1 = {'a': 100, 'b': 200, 'c':300}
Program:
import pandas as pd
dict1={'a':100,'b':200,'c':300}
print('Original Dictionary')
print(d1)
new_series= pd.Series(d1)
print('Converted Series :')
print(new_series)
Input:
Output:
20
Program:
import pandas as pd
S=pd.Series([400,300.12,100,200])
print('Original Data Series:')
print(S)
new_series = pd.Series(S).sort_values()
print(new_series)
Input:
21
Output:
22
Program:
import pandas as pd
import numpy as np
a=np.array([[33,44,56,67,88],[22,79,60,75,62],[21,42,63,84,95],
[86,87,88,89,74],[97,86,77,68,60],[41,59,86,53,60],
[49,67,37,13,48]])
b=pd.DataFrame(p)
print('Given DataFrame:')
print(b)
print('--------------------------------')
print('First three rows:')
print(b.head(3))
Input:
23
Output:
24
Experiment No. 13:
Write a Pandas program to sort the DataFrame first by 'name' in
descending order, then by 'score' in ascending order.
Program:
import pandas as pd
import numpy as np
exam_data={'name':['Swastik','Jain','Kate','Johnny','Clark','Stark',
'Malinga','Buttler','Smith','Sehwag'],
'score':[12,9,16.5,np.NaN,9,20,14.5,np.NaN,8,19],
'attempts':[5,2,2,1,2,3,1,3,3,2],
'qualify':['yes','no','yes','no','no','yes','yes','no','no','yes']}
labels=['a','b','c','d','e','f','g','h','i','j']
x=pd.DataFrame(exam_data,labels)
print('Original rows:')
print(x)
x.sort_values(by=['name','score'],ascending=[False,True],inplace=True)
print('Sort the DataFrame first by name in descending order and score
by ascending order')
print(x)
Input:
25
Output:
26
Experiment No. 14:
Write a Pandas program to insert a new column in existing DataFrame.
Program:
import pandas as pd
r=pd.Series([5,7,17,6,1])
df=pd.DataFrame(r,columns=['Month_No'])
print('The existing dataFrame:\n',df)
df['No_of_days']=[30,31,31,31,31]
print('After inserting a column:')
print(df)
Input:
Output:
27
Program:
import pandas as pd
s1= pd.Series(['180','250','python','310.12','430'])
s2= pd.Series(['11','30','php','36.12','20'])
print("\nSeries 1:")
print(s1)
print("\nSeries 2:")
print(s2)
df = pd.concat([s1,s2],axis=1)
print("\nNew DataFrame combining two series:")
print(df)
Input:
28
Output:
29
Experiment No. 16:
Given a Series that stores the area of some states in sq km. Write code
to find out the biggest and smallest three areas from the given Series.
Ser5=[34567, 890, 450, 67892, 34677, 78902, 256711, 678291, 637632,
25723, 2367, 11789, 345, 256517].
Program:
import pandas as pd
Ser5=pd.Series([34567,890,450,67892,34677,78902,256711,637632,25
723,2367,11789,345,256517])
print('3 Biggest areas are :')
print(Ser5.sort_values(ascending=True).head(3))
print('3 smallest areas are :')
print(Ser5.sort_values(ascending=False).head(3))
Input:
Output:
30
Experiment No. 17:
Given an ndarray p as ([1, 2, 3, 4]). Write code to plot a bar chart
having bars for p and p**2 (with red colour) and another bar for p and
p*2 (with blue colour).
Program:
import numpy as np
import matplotlib.pyplot as plt
p=np.array([1,2,3,4])
plt.bar(p,p**2,color ='r',width = 0.6)
plt.bar(p+0.3,p*2,color ='b',width = 0.6)
Input:
Output:
31
Experiment No. 18:
Create a pandas series from a dictionary of values and a ndarray.
Program:
import pandas as pd
dictionary={'A':830,'B':380,'C':700}
series=pd.Series(dictionary)
print(series)
Input:
Output:
32