Thursday 11 March 2021

Class 12 IP Practical - 21

 


21. WAP to  create a DataFrame to store weight, age and names of 3 people. Print the DataFrame and its transpose.

import pandas as pd

d={'Weight':[75,72,68],'Age':[25,23,21],'Name':['Ram','Raj','Sam']}

Df=pd.DataFrame(d)

print("DataFrame is : ")

print(Df)

print("Transpose of DataFrame is : ")

print(Df.T)

                     

 

>>>

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

DataFrame is :

   Weight  Age Name

0      75   25  Ram

1      72   23  Raj

2      68   21  Sam

Transpose of DataFrame is :

          0    1    2

Weight   75   72   68

Age      25   23   21

Name    Ram  Raj  Sam

>>> 

 











Class 12 IP Practical - 20

 

20. Consider the following DataFrame saleDf

 

Target

Sales

ZoneA

56000

58000

ZoneB

70000

68000

ZoneC

75000

78000

ZoneD

60000

61000

WAP a program to add a column namely Orders having values 6000, 6700, 6200 and 6000 respectively for the zones A, B, C and D. The program should also add a new row for a new zone ZoneE. Add some dummy values in this row.

 import pandas as pd

d={'Target':{'ZoneA':56000,'ZoneB':70000,'ZoneC':75000,'ZoneD':60000}, \

   'Sales': {'ZoneA':58000,'ZoneB':68000,'ZoneC':78000,'ZoneD':61000}}

saleDf=pd.DataFrame(d)

print("DataFrame: ")

print(saleDf)

saleDf['Orders']=[6000, 6700, 6200 , 6000]

saleDf.at['ZoneE',:]=[70000,58000,6100]

print("Updated DataFrame: ")

print(saleDf)

 

 

>>>

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

DataFrame:

       Target  Sales

ZoneA   56000  58000

ZoneB   70000  68000

ZoneC   75000  78000

ZoneD   60000  61000

Updated DataFrame:

        Target    Sales  Orders

ZoneA  56000.0  58000.0  6000.0

ZoneB  70000.0  68000.0  6700.0

ZoneC  75000.0  78000.0  6200.0

ZoneD  60000.0  61000.0  6000.0

ZoneE  70000.0  58000.0  6100.0

>>> 

 

 











Class 12 IP Practical - 19

 

19. WAP in Python to create a the following DataFrame –

 

Population

Hospitals

Schools

Delhi

10927986.0

189.0

7916.0

Mumbai

12691836.0

208.0

8508.0

Kolkata

46192.0

149

7226.0

Chennai

4328063.0

157

7617.0

Banglore

5678097.0

1200.0

1200.0

Create another DataFrame from the above DataFrame which not contains column ‘Population’ and raw Banglore.


import pandas as pd

d={'Population':{'Delhi':10927986.0,'Mumbai':12691836.0,'Kolkata':46192.0, \

                 'Chennai':4328063.0,'Banglore':5678097.0}, \

   'Hospitals':{'Delhi':189.0,'Mumbai':208.0,'Kolkata':149,'Chennai':157,'Banglore':1200.0}, \

   'Schools':{'Delhi':7916.0,'Mumbai':8508.0,'Kolkata':7226.0, \

                 'Chennai':7617.0,'Banglore':1200.0}}

Df=pd.DataFrame(d)

print("DataFrame is : ")

print(Df)

Df1=Df.drop(index='Banglore', columns='Population')

print("Updated DataFrame: ")

print(Df1)

 

 

 

>>>

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

DataFrame is :

          Population  Hospitals  Schools

Delhi     10927986.0      189.0   7916.0

Mumbai    12691836.0      208.0   8508.0

Kolkata      46192.0      149.0   7226.0

Chennai    4328063.0      157.0   7617.0

Banglore   5678097.0     1200.0   1200.0

Updated DataFrame:

         Hospitals  Schools

Delhi        189.0   7916.0

Mumbai       208.0   8508.0

Kolkata      149.0   7226.0

Chennai      157.0   7617.0

>>> 

 








Class 12 IP Practical - 18

 

18. Consider the saleDf shown below.

 

Target

Sales

ZoneA

56000

58000

ZoneB

70000

68000

ZoneC

75000

78000

ZoneD

60000

61000

Write a program to rename indexes of ZoneC and ZoneD as Central and Dakshin respectively and the column names Target and Sales as Targeted and Achieved respectively.

 

import pandas as pd

d={'Target':{'ZoneA':56000,'ZoneB':70000,'ZoneC':75000,'ZoneD':60000}, \

   'Sales': {'ZoneA':58000,'ZoneB':68000,'ZoneC':78000,'ZoneD':61000}}

saleDf=pd.DataFrame(d)

print(saleDf)

salesDf1=saleDf.rename(index={'ZoneC':'Central','ZoneD':'Dakshin'}, \

                    columns={'Target':'Targeted','Sales':'Achieved'})

print(salesDf1)


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

       Target  Sales

ZoneA   56000  58000

ZoneB   70000  68000

ZoneC   75000  78000

ZoneD   60000  61000

         Targeted  Achieved

ZoneA       56000     58000

ZoneB       70000     68000

Central     75000     78000

Dakshin     60000     61000

>>> 

 











Class 12 IP Practical - 17

 

#17. WAP in Python to create a DataFrame to store weight, age and names of 3 people. Print the DataFrame and its transpose.


import pandas as pd

d={'Weight':[75,72,65], \

   'Age':[28,22,32], \

   'Name':['Ashu','Ritesh','Krishan']}

df=pd.DataFrame(d)

print("DataFrame is: ")

print(df)

print("Transpose of DataFrame is: ")

print(df.T)

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

DataFrame is:

   Weight  Age     Name

0      75   28     Ashu

1      72   22   Ritesh

2      65   32  Krishan

Transpose of DataFrame is:

           0       1        2

Weight    75      72       65

Age       28      22       32

Name    Ashu  Ritesh  Krishan

>>> 

 






Class 12 IP Practical - 16

 

#16. WAP to transpose the values of index and columns in a DataFrame.

import pandas as pd

d={'Hindi':{'Ram':30,'Raj':20,'Sam':25,'John':35}, \

     'English':{'Ram':35,'Raj':25,'Sam':22,'John':32}, \

     'IP':{'Ram':40,'Raj':45,'Sam':45,'John':48}, \

     'Bio':{'Ram':30,'Raj':24,'Sam':25,'John':25}}

df=pd.DataFrame(d)

print("Data Frame is :")

print(df)

print("Transpose of Data Frame is :")

print(df.T)


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

Data Frame is :

      Hindi  English  IP  Bio

Ram      30       35  40   30

Raj      20       25  45   24

Sam      25       22  45   25

John     35       32  48   25

Transpose of Data Frame is :

         Ram  Raj  Sam  John

Hindi     30   20   25    35

English   35   25   22    32

IP        40   45   45    48

Bio       30   24   25    25

>>> 

 






Class 12 IP Practical - 10

 

#10. Create a data frame using dictionary.

import pandas as pd

d1={'Hindi':{'Ram':30,'Raj':20,'Sam':25,'John':35}, \

     'English':{'Ram':35,'Raj':25,'Sam':22,'John':32}, \

     'IP':{'Ram':40,'Raj':45,'Sam':45,'John':48}, \

     'Bio':{'Ram':30,'Raj':24,'Sam':25,'John':25}}

 

df=pd.DataFrame(d1)

print(df)




 

========= RESTART: C:/Program Files (x86)/Python38-32/ip-7.py =========

      Hindi  English  IP  Bio

Ram      30       35  40   30

Raj      20       25  45   24

Sam      25       22  45   25

John     35       32  48   25

>>> 

 


Class 12 IP Practical - 09

 

#9. Create a Series using dictionary.


import pandas as pd

emp={'Id':101,'Name':'Ram','Salary':25000,'Age':25}

S=pd.Series(emp)

print(S)




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

Id          101

Name        Ram

Salary    25000

Age          25

dtype: object

>>> 

 


Class 12 IP - Practical - 04

 

#4. Write a Python Program to plot two or more lines with legends, different width, color, marker and style.


import matplotlib.pyplot as plt

x1=[3,5,7,9,11]

y1=[2,4,6,8,10]

x2=[11,9,7,5,3]

y2=[3,5,7,11,9]

x3=[7,11,5,7,6]

y3=[3,5,7,11,9]

plt.plot(x1,y1,label="Line1",color="r",linewidth="2",linestyle="dotted", \

         marker="o",markersize=5,markeredgecolor="blue")

plt.plot(x2,y2,label="Line2",color="b",linewidth="2.5",linestyle="dashed", \

         marker="D",markersize=6,markeredgecolor="green")

plt.plot(x3,y3,label="Line3",color="g",linewidth="3.5",linestyle="solid", \

         marker="d",markersize=4,markeredgecolor="red")

plt.legend()

plt.title("Multiline Chart")

plt.xlabel("X Axis")

plt.ylabel("Y Axis")

plt.show()













Class 12 IP Practical - 03

 

 WAP in Python to compare the sugar levels among men and women in a city using histogram.

Men:[113,85,90,150,149,88,93,115,135,80,77,82,129]

Women:[67,98,120,133,150,84,69,89,79,120,112,100]

bins:[80,100,125,150]


import matplotlib.pyplot as plt


Men=[113,85,90,150,149,88,93,115,135,80,77,82,129]

Women=[67,98,120,133,150,84,69,89,79,120,112,100]

bins=[80,100,125,150]

plt.hist([Men,Women],bins)

plt.xlabel("Intervals")

plt.ylabel("Number of Person")

plt.title("Histogram Chart")

plt.show()