You are on page 1of 6

CLASS XII

INFORMATICS PRACTICES
PRACTICAL LIST
PYTHON
S.No. Description Date of
submission
1 Write a NumPy program to create a 3x3 matrix with values ranging from 2 to10 31 Jul
Ans : import numpy as np 2019
arr1 = np.arange(2,11).reshape(3,3)
print(arr1)
2 Write a NumPy program to generate six random integers between 25 and55. 31 Jul
Ans : import numpy as np 2019
arr1 = np.random.randint(low = 25, high = 55, size = 6)
print(arr1)
3 Write a Pandas program to convert a Panda Series to Python list . 31 Jul
Ans : import pandas as pd 2019
ds = pd.Series([2,4,6,8,10])
print('Pandas Series and type')
print(ds)
print(type(ds))
print('Convert Pandas Series to Python list')
print(ds.tolist())
print(type(ds.tolist()))
4 Write a Pandas program to compare the elements of the two PandasSeries?? 31 Jul
Ans: import pandas as pd 2019
ds1 = pd.Series([2,4,6,8,10])
ds2 = pd.Series([1,3,5,7,10])
print('Series1:')
print(ds1)
print('Series2:')
print(ds2)
print('Compare the elements of Series:')
print('Equals:')
print(ds1 == ds2)
print('Greater than:')
print(ds1>ds2)
print('Less than:')
print(ds1<ds2)
5 Write a python program to reverse a NumPy array
Ans : import numpy as np
arr1 = np.array([12,45,13,45,56,38])
print('Original array:')
print(arr1)
print('Reverse Array:')
arr1 = arr1[::-1]
print(arr1)
6 Write a Pandas program to add, subtract, multiple and divide two PandasSeries
Ans :import pandas as pd
ds1 = pd.Series([2,4,6,8,10])
ds2 = pd.Series([1,3,5,7,9])
print(ds1)
print(ds2)
ds = ds1 + ds2
print('Add two Series:')
print(ds)
print('Subtract two Series:')
ds = ds1 - ds2
print(ds)
print('Multiply two Series:')
ds = ds1 * ds2
print(ds)
print('Divide Series1 by Series2:')
ds = ds1/ds2
print(ds)
7. Write a python program to create a Series using list
Ans : import pandas as pd
lst = [2,4,6,8,10]
print('List')
print(lst)
print(type(lst))
ds = pd.Series(lst)
print('Series')
print(ds)
print(type(ds))
8 Write a python program to create a Dataframe using dictionary.
Ans : import pandas as pd
diSal =
{2016:{'Qtr1':2000,'Qtr2':5000,'Qtr3':10000,'Qtr4':12000},

2017:{'Qtr1':7000,'Qtr2':4000,'Qtr3':10000,'Qtr4':14000},

2018:{'Qtr1':6000,'Qtr2':9000,'Qtr3':15000,'Qtr4':12000},
2019:{'Qtr1':8000,'Qtr2':12000,'Qtr3':12000,‘Qtr4’:10000}}

sal_df = pd.DataFrame(diSal)
print('DataFrame')
print(sal_df)
9 Write a python program to create two Dataframe and demonstrate usage of following
function.
 add
 sub
 mul
 div
Ans :import pandas as pd
dict1 = {'A':[1,2,3],
'B':[4,5,6],
'C' : [7,8,9]}

dict2 = {'A':[10,20,30],
'B':[40,50,60],
'C' : [70,80,90]}
dtf1 = pd.DataFrame(dict1)
dtf2 = pd.DataFrame(dict2)
print(dtf1)
print(dtf2)
print('perform addition')
print(dtf1.add(dtf2))
print('perform subtraction')
print(dtf2.sub(dtf1))
print('perform multiplication')
print(dtf1.mul(dtf2))
print('perform division')
print(dtf2.div(dtf1))
10 Write a python program to create a Dataframe and demonstrate
 iterrows

Ans : import pandas as pd


diSal =
{2016:{'Qtr1':2000,'Qtr2':5000,'Qtr3':10000,'Qtr4':12000},

2017:{'Qtr1':7000,'Qtr2':4000,'Qtr3':10000,'Qtr4':14000},

2018:{'Qtr1':6000,'Qtr2':9000,'Qtr3':15000,'Qtr4':12000},

2019:{'Qtr1':8000,'Qtr2':12000,'Qtr3':12000,'Qtr4':10000}}

df = pd.DataFrame(diSal)
print(df)
print(df)
print()
for i, j in df.iterrows():
print('Row index:',i)
print('Containing')
print(j)
print()
11 Write a python program to create a Dataframe and demonstrate
 iteritems
Ans : import pandas as pd
diSales =
{2015:{'Qtr1':34500,'Qtr2':56000,'Qtr3':47000,'Qtr4':49000},

2016:{'Qtr1':44900,'Qtr2':46100,'Qtr3':57000,'Qtr4':59000},

2017:{'Qtr1':54500,'Qtr2':51000,'Qtr3':57000,'Qtr4':58500}}
dtf1 = pd.DataFrame(diSales)
print('DataFrame')
print(dtf1)
print()

for(col,colSeries) in dtf1.iteritems():
print('Column index:',col)
print('Containing')
print(colSeries)
12 Write a python program that create a dataframe and find
 mode
 mean
 median
Ans : import pandas as pd
dict1 =
{2016:{'Qtr1':2000,'Qtr2':13000,'Qtr3':10000,'Qtr4':12000},

2017:{'Qtr1':7000,'Qtr2':10000,'Qtr3':10000,'Qtr4':14000},

2018:{'Qtr1':6000,'Qtr2':15000,'Qtr3':15000,'Qtr4':12000},
2019:{'Qtr1':7000,'Qtr2':12000,'Qtr3':12000}}
sal_df = pd.DataFrame(dict1)
print(sal_df)
print()

print('mode is:')
print(sal_df.mode())
print(sal_df.mode(axis = 1))
print()

print('median is:')
print(sal_df.median())
print(sal_df.median(axis = 1))
print()

print('mean is:')
print(sal_df.mean())
print(sal_df.mean(axis = 1))
13 Write a NumPy program to create a 8x8 matrix and fill it with a checkboard pattern
Checkboard pattern:
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
Ans : import numpy as np

x = np.zeros((8,8),dtype=int)
print('aaray matrix x')
print(x)
print()

print("Checkerboard pattern:")
x[1::2,::2] = 1
x[::2,1::2] = 1
print(x)
14 Write a python program that create a dataframe and demonstrate usage of pivot table.
Ans : import pandas as pd
d1 = {'Tutor':['Tahira','Gurjot','Anusha','Jacob','Venkat'],
'Classes':[28,36,41,32,40],
'Country':['USA','UK','Japan','USA','Brazil']}
dfd = pd.DataFrame(d1)
print('Tutor Data:')
print(dfd)
print()

print('Data in Pivote table:')


print(dfd.pivot(index = 'Tutor', columns = 'Country', values
= 'Classes'))
15 Write a python program to create a Series sort the elements of the given series object in
descending order.
Ans : import pandas as pd
# Creating the Series
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])

print(sr)

# sort the values in descending order


print(sr.sort_values(ascending = False) )
16 Write a Pandas program to sort the data frame first by 'Designation' in descending order, then
by 'Name' in Ascending order.
Ans :import pandas as pd

data1 = {'Name':['Vijay', 'Sameer', 'Pihu', 'Neha','Ali'] ,


'Age':[28,34,29,42,43],
'Designation':['Accountant' , 'Clerk', 'Clerk',
'Manager','Accountant']}
df1 = pd.DataFrame(data1)
print('Original Data')
print(df1)
print()

print('Sorted data')
print (df1.sort_values(by=['Designation','Name'],
ascending=[False,True]))

17 Draw the bar chart from the following data given


Over : 1,2,3,4,5
Run by India in over : 10,3,14,15,4
Run by Bangladesh : 4,9,3,8,10
Ans : import matplotlib.pyplot as plt
import numpy as np
over = np.arange(1,6,1)
India = [10,3,14,15,4]
Pak = [4,9,3,8,10]
plt.title('Ind v/s Pak')
plt.bar(over,India, color = 'b', width = 0.25, label =
'India')
plt.bar(over + 0.25, Pak, color = 'r', width = 0.25, label
= 'Pakistan')
plt.legend(loc = 'upper left')
plt.xlabel("Overs")
plt.ylabel("Runs")
plt.show()

You might also like