Thursday 11 March 2021

Class 12 IP Practical - 15

 15. WAP to represent size and shape of the  DataFrame.


import pandas as pd

d={'Ritesh':{'English':85,'Bio':82,'Physics':35,'IP':95,'Chemistry':81}, \

   'Rishabh':{'English':55,'Bio':62,'Physics':55,'IP':85,'Chemistry':91}, \

   'Harshit':{'English':65,'Bio':72,'Physics':75,'IP':91,'Chemistry':71}, \

   'Krishan':{'English':25,'Bio':75,'Physics':45,'IP':93,'Chemistry':61}, \

   'Shailesh':{'English':75,'Bio':82,'Physics':65,'IP':97,'Chemistry':51}}

df1=pd.DataFrame(d)

print("DataFrame is:")

print(df1)

print("Size of DataFrame:")

print(df1.size)

print("Shape of DataFrame:")

print(df1.shape)



>>>

========= RESTART: C:/Program Files (x86)/Python38-32/Ip-15.py ========

DataFrame is:

           Ritesh  Rishabh  Harshit  Krishan  Shailesh

English        85       55       65       25        75

Bio            82       62       72       75        82

Physics        35       55       75       45        65

IP             95       85       91       93        97

Chemistry      81       91       71       61        51

Size of DataFrame:

25

Shape of DataFrame:

(5, 5)

>>> 








Class 12 IP Practical - 14

 14. WAP to show indexes and axes of a DataFrame.


 

import pandas as pd

d={'Ritesh':{'English':85,'Bio':82,'Physics':35,'IP':95,'Chemistry':81}, \

   'Rishabh':{'English':55,'Bio':62,'Physics':55,'IP':85,'Chemistry':91}, \

   'Harshit':{'English':65,'Bio':72,'Physics':75,'IP':91,'Chemistry':71}, \

   'Krishan':{'English':25,'Bio':75,'Physics':45,'IP':93,'Chemistry':61}, \

   'Shailesh':{'English':75,'Bio':82,'Physics':65,'IP':97,'Chemistry':51}}

df1=pd.DataFrame(d)

print("DataFrame is:")

print(df1)

print("Index of DataFrame:")

print(df1.index)

print("Axis of DataFrame:")

print(df1.axes)

>>>

========= RESTART: C:/Program Files (x86)/Python38-32/IP-14.py ========

DataFrame is:

           Ritesh  Rishabh  Harshit  Krishan  Shailesh

English        85       55       65       25        75

Bio            82       62       72       75        82

Physics        35       55       75       45        65

IP             95       85       91       93        97

Chemistry      81       91       71       61        51

Index of DataFrame:

Index(['English', 'Bio', 'Physics', 'IP', 'Chemistry'], dtype='object')

Axis of DataFrame:

[Index(['English', 'Bio', 'Physics', 'IP', 'Chemistry'], dtype='object'), Index(['Ritesh', 'Rishabh', 'Harshit', 'Krishan', 'Shailesh'], dtype='object')]

>>> 

Class 12 IP Practical - 13

 13.  WAP to perform mathematical operation addition, subtraction and multiplication on two series.

 


import pandas as pd

d1=[10,20,30,40,50,60,70,80]

S1=pd.Series(d1)

d2=[1,2,3,4,5,6,7,8]

S2=pd.Series(d2)

print("First Series:")

print(S1)

print("2nd Series:")

print(S2)

print("Addition of S1+S2 Series:")

print(S1.add(S2))

print("Subtraction of S1-S2 Series:")

print(S1.sub(S2))

print("Multiplication of S1*S2 Series:")

print(S1.mul(S2))



>>>

========= RESTART: C:/Program Files (x86)/Python38-32/Ip-13.py ========

First Series:

0    10

1    20

2    30

3    40

4    50

5    60

6    70

7    80

dtype: int64

2nd Series:

0    1

1    2

2    3

3    4

4    5

5    6

6    7

7    8

dtype: int64

Addition of S1+S2 Series:

0    11

1    22

2    33

3    44

4    55

5    66

6    77

7    88

dtype: int64

Subtraction of S1-S2 Series:

0     9

1    18

2    27

3    36

4    45

5    54

6    63

7    72

dtype: int64

Multiplication of S1*S2 Series:

0     10

1     40

2     90

3    160

4    250

5    360

6    490

7    640

dtype: int64

>>> 


Class 12 IP Practical - 12

 12.  Create a Series and access the top 2 and last 2 elements using head() and tail() methods.


import pandas as pd

d=[10,20,30,40,50,60,70,80]

S=pd.Series(d)

print("Series:")

print(S)

print("Top 2 Elements of Series:")

print(S.head(2))

print("Last 2 Elements of Series:")

print(S.tail(2))



 

========= RESTART: C:/Program Files (x86)/Python38-32/IP-12.py ========

Series:

0    10

1    20

2    30

3    40

4    50

5    60

6    70

7    80

dtype: int64

Top 2 Elements of Series:

0    10

1    20

dtype: int64

Last 2 Elements of Series:

6    70

7    80

dtype: int64

>>>  

Class 12 IP Practical - 11

 11.  WAP in Python to sort the pandas DataFrame on the basis of a single column in ascending order.



 

import pandas as pd

d={'Ritesh':{'English':85,'Bio':82,'Physics':35,'IP':95,'Chemistry':81}, \

   'Rishabh':{'English':55,'Bio':62,'Physics':55,'IP':85,'Chemistry':91}, \

   'Harshit':{'English':65,'Bio':72,'Physics':75,'IP':91,'Chemistry':71}, \

   'Krishan':{'English':25,'Bio':75,'Physics':45,'IP':93,'Chemistry':61}, \

   'Shailesh':{'English':75,'Bio':82,'Physics':65,'IP':97,'Chemistry':51}}

df1=pd.DataFrame(d)

print("DataFrame is:")

print(df1)

print("Sorted DataFrame w.r.t. IP is:")

print(df1.sort_values('Harshit',axis=0))

                                         



 

>>>

========= RESTART: C:/Program Files (x86)/Python38-32/IP-11.py ========

DataFrame is:

           Ritesh  Rishabh  Harshit  Krishan  Shailesh

English        85       55       65       25        75

Bio            82       62       72       75        82

Physics        35       55       75       45        65

IP             95       85       91       93        97

Chemistry      81       91       71       61        51

Sorted DataFrame w.r.t. IP is:

           Ritesh  Rishabh  Harshit  Krishan  Shailesh

English        85       55       65       25        75

Chemistry      81       91       71       61        51

Bio            82       62       72       75        82

Physics        35       55       75       45        65

IP             95       85       91       93        97

>>>  

Class 12 IP Practical - 08

 

#8 - Write a program to plot a bar chart from the medals won by the four countries. Make sure that bars are separately visible.                                  

Country

Gold

Silver

Bronze

Total

India

26

20

20

66

Australia

80

59

59

198

England

45

45

46

136

Canada

10

12

14

36

  

import matplotlib.pyplot as plt

import numpy as np

info=['Gold','Silver','Bronze','Total']

India=[26,20,20,66]

Australia=[80,59,59,198]

England=[45,45,46,136]

Canada=[10,12,14,36]

plt.figure(figsize=(10,7))

x=np.arange(len(info))

plt.bar(info,India,width=0.15,label="India")

plt.bar(x+0.15,Australia,width=0.15,label="Australia")

plt.bar(x+0.30,England,width=0.15,label="England")

plt.bar(x+0.45,Canada,width=0.15,label="Canada")

plt.xlabel("Medal Type")

plt.ylabel("Number of Medals won")

plt.title("Medal Tally Bar Chart")

plt.legend()

plt.show()

 





Class 12 IP Practical - 07

 

#7 - Given a series nfib that contains reversed Fibonacci numbers with Fibonacci numbers as show 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 nfib 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


import matplotlib.pyplot as plt

import numpy as np

nfib=[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),nfib,'mo',markersize=5,linestyle='solid',markeredgecolor='k')

plt.grid(True)

plt.xlabel("X Axis")

plt.ylabel("Y Axis")

plt.title("Reversed Fibonacci Numbers with Fibonacci Series")

plt.legend()

plt.show()







Class 12 IP Practical - 06

 

#6 - First 10 terms of Fibonacci series are stored in a list namely fib:          

 

fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

 

Write a program to plot Fibonacci terms and their square-roots with two separate lines on the same plot.

(a)    Series should be plotted as a cyan line with ‘o’ markers having size as 5 and edge-color as red.

(b)   The square-root series should be plotted as a black line with ‘+’ markers having size as 7 and edge-color as red.

 

 

import matplotlib.pyplot as plt

import numpy as np

fib=[0,1,1,2,3,5,8,13,21,34]

sqfib=np.sqrt(fib)

plt.plot(range(1,11),fib,'co',markersize=5,linestyle='solid',markeredgecolor='r',label="fib")

plt.plot(range(1,11),sqfib,'k+',markersize=7,linestyle='solid',markeredgecolor='r',label="sqfib")

plt.xlabel("X Axis")

plt.ylabel("Y Axis")

plt.title("Fibonacci Series")

plt.legend()

plt.show()

 











Class 12 IP Practical - 05

 

#5 - WAP in Python to create a bar chart by using multiple x values on the same chart for men and women.

men: (22,30,35,35,26)

women: (25,32,30,35,29)

 

import matplotlib.pyplot as plt

import numpy as np

men= (22,30,35,35,26)

women= (25,32,30,35,29)

x1=np.arange(2,21,4)

plt.bar(x1,men,color='r',label='Men')

plt.bar(x1+2,women,color='b',label='Women')

plt.legend()

plt.xlabel("X Axis")

plt.ylabel("Y Axis")

plt.title("Bar Chart")

plt.show()