Xii Ip Practical File 24-25.Docx

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 111

XIITH STD PRACTICAL FILE

SUBJECT: INFORMATICES PRACTICESWITH PYTHON


SUB.CODE:065

ACADEMIC YEAR 2024-2025


PROGRAM 1
import pandas as pd
import numpy as np
data=np.arange(2,25,2)
narr=np.array(data)
srs=pd.Series(narr)
print(srs)

OUTPUT:

0 2

1 4

2 6

3 8

4 10

5 12

6 14

7 16

8 18

9 20

10 22

11 24

dtype: int32

>>>
PROGRAM 2

Write the code to create the series ‘serObj’ and answer the questions followed.
Jan 31 Feb 28 Mar 31 Apr 30
1. Write the command to add one row: ‘May’ – 31
2. Write the command to update Feb to 29
3. Write the command to change index to 1,2,3,4,5 in place of Jan,Feb,Mar, Apr and May.
4. Write a command to print a month name having number of days less than 31.
5. Write the output:
a) print(serObj<30)
b) print(serObj + 3)

import pandas as pd
serObj=pd.Series([31,28,31,30], index=['Jan','Feb','Mar','Apr'])
serObj['May']=31
serObj['Feb']=29
serObj.index=[1,2,3,4,5]
serObj[serObj<31]
print(serObj < 30)
print(serObj+3)
print(serObj)

output

1 False
2 True
3 False
4 False
5 False
dtype: bool
1 34
2 32
3 34
4 33
5 34
dtype: int64
1 31
2 29
3 31
4 30
5 31
dtype: int64
PROGRAM 3

Write a Pandas program to perform arithmetic operations on two Pandas Series.

import pandas as pd
ds1 = pd.Series([3, 6, 9, 12, 15])
ds2 = pd.Series([2, 4, 6, 8, 10])
ds = ds1 + ds2
print(ds1, "\n", 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)

output
0 3
1 6
2 9
3 12
4 15
dtype: int64
0 2
1 4
2 6
3 8
4 10
dtype: int64
Add two Series:
0 5
1 10
2 15
3 20
4 25
dtype: int64
Subtract two Series:
0 1
1 2
2 3
3 4
4 5
dtype: int64
Multiply two Series:
0 6
1 24
2 54
3 96
4 150
dtype: int64
Divide Series1 by Series2:
0 1.5
1 1.5
2 1.5
3 1.5
4 1.5
dtype: float64
PROGRAM 4
import pandas as pd

n=int(input('enter the number of

elements'))dict1={}

for i in range(n):

key=input('entername')

value=int(input('enter the total marks out of 500'))

dict1[key]=value

print('new dictionary')

print('**************')

print(dict1)

srs=pd.Series(dict1)print()

print('series using dictionary')

print('**********************')

print(srs)print()

l1=[]

n1=int(input('enter the number of elements'))

for j in range(n1):

ele=int(input('enter the element'))

l1.append(ele)

print('new list')

print('********')

print(l1)
import numpy as n

arr=n.array(l1)

print()

print('series using nd array')

print('*********************')

srs1=pd.Series(arr)

print(srs1)

OUTPUT

enter the number of elements3

enter name MATEEN

enter the total marks out of 500/460

enter name HASAN

enter the total marks out of 500/355

enter name ZIA

enter the total marks out of 500/480

new dictionary

**************

{'MATEEN': 460, 'HASAN': 355, 'ZIA': 480}

series using dictionary

***********************

MATEEN 460
HASAN 355

ZIA 480
dtype: int64
enter the number of elements 3

enter the element100

enter the element200

enter the element500

new list

********

[100, 200, 500]

series using nd array

*********************

0 100

1 200

2 500

dtype: int32

>>>
PROGRAM 5

import pandas as pd

import numpy as np

dont=np.array([6700,5600,5000,5200,np.NAN])

sec=['A','B','C','D','E']

srs=pd.Series(dont*2,index=sec,dtype=np.float32)

print(srs)

OUTPUT

A 13400.0

B 11200.0

C 10000.0

D 10400.0

E NaN

dtype: float32

>>>
PROGRAM 6

import pandas as pd

area=[3456,7879,2343,897,9877,568,7954,2325,6578,535]

ind=['kerala','rajasthan','madhyapradesh','tamilnadu','maharashtra','karnataka','goa','guj
arat','bihar','andhrapradesh']

srs=pd.Series(area,index=ind)

print('original series')

print('***************')

print(srs)

print()

print('sorted series')

print('*************')

srs=srs.sort_values()

print(srs)

print()

print('first 3 smallest areas')

print('**********************')

print(srs.head(3))

print()

print('last 3 biggest areas')

print('********************')

print(srs.tail(3))
OUTPUT

original series

***************

kerala 3456

rajasthan 7879

madhyapradesh 2343

tamilnadu 897

maharashtra 9877

karnataka 568

goa 7954

gujarat 2325

bihar 6578

andhrapradesh 535

dtype: int64

sorted series

*************

andhrapradesh 535

karnataka 568

tamilnadu 897

gujarat 2325

madhyapradesh 2343

kerala 3456

bihar 6578

rajasthan 7879

goa 7954

maharashtra 9877
dtype: int64

first 3 smallest areas

**********************

andhrapradesh 535
karnataka 568

tamilnadu 897
dtype: int64

last 3 biggest areas

********************

rajasthan 7879

goa 7954

maharashtra 9877

dtype: int64

>>>
PROGRAM 7

import pandas as pd

age=[45,60,34,78,56,48,12,25,67,82]

srs=pd.Series(age,index=['mateen','zia','abdur','mohammed','ahmed','hasan','syed','abdu
llah','zaid','faiz'])

print(srs)

print()

print('persons with age above 50')

print('*************************')

print(srs[srs>50])

OUTPUT

mateen 45

zia 60

abdur 34

mohammed 78

ahmed 56

hasan 48

syed 12

abdullah 25

zaid 67

faiz 82
dtype: int64
persons with age above 50

*************************

zia 60

mohammed 78

ahmed 56

zaid 67

faiz 82

dtype: int64

>>>
PROGRAM 8

import pandas as pd

import numpy as np

dict1={'items':['pencil','notebook','pen','eraser','scale'],'cp':[10,25,8,5,12],'sp':[8,22,8,5,
10],'discount':[2,3,np.NaN,np.NaN,2]}

item=pd.DataFrame(dict1,index=np.arange(1,6))

print()

print('\t ITEMLIST')

print('\t ********')

print(item)

OUTPUT

ITEMLIST

********

items cp sp discount
1 pencil 10 8 2.0

2 notebook 25 22 3.0

3 pen 8 8 NaN

4 eraser 5 5 NaN

5 scale 12 10 2.0
>>>
PROGRAM 9

Write the code to create a DataFrame ‘RESULT’ and answer the questions followed.

1. Write a command to add one row T5 with values 75.6, 98.6, 56.0
2. Write a command to add one column Total = col1+col2+col3
3. Write a command to change the column names Col1 to Maths, Col2 to Science, Col3 to SST.
4. Write a command to print Score of Maths and Science only.
5. Write a command to update NaN to 85.0 in T3 Row and Col1

import pandas as pd

import numpy as np

data = {'Col1': [100.0, 95.8, np.nan, 82.0], 'Col2': [100.0, 100.0, 100.0, 85.4], 'Col3': [60.0,
57.48, 53.48, 49.20]}

index = ['T1', 'T2', 'T3', 'T4']

RESULT = pd.DataFrame(data, index)

print(RESULT)

RESULT.loc['T5'] = [75.6, 98.6, 56.0]

RESULT['Total'] = RESULT['Col1'] +RESULT['Col2'] + RESULT['Col3']

RESULT = RESULT.rename(columns={'Col1': 'Maths', 'Col2': 'Science', 'Col3': 'SST'})

print(RESULT)

print(RESULT[['Maths', 'Science']])

RESULT.at['T3', 'Maths'] = 85.0

print(RESULT)
output
Col1 Col2 Col3
T1 100.0 100.0 60.00
T2 95.8 100.0 57.48
T3 NaN 100.0 53.48
T4 82.0 85.4 49.20

Maths Science SST Total


T1 100.0 100.0 60.00 260.00
T2 95.8 100.0 57.48 253.28
T3 NaN 100.0 53.48 NaN
T4 82.0 85.4 49.20 216.60
T5 75.6 98.6 56.00 230.20

Maths Science
T1 100.0 100.0
T2 95.8 100.0
T3 NaN 100.0
T4 82.0 85.4
T5 75.6 98.6

Maths Science SST Total


T1 100.0 100.0 60.00 260.00
T2 95.8 100.0 57.48 253.28
T3 85.0 100.0 53.48 NaN
T4 82.0 85.4 49.20 216.60
T5 75.6 98.6 56.00 230.20
PROGRAM 10
# Write a Pandas program to select the rows where the percentage greater than 70 and
between 70,90
import pandas as pd
import numpy as np

exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew', 'Kartik',
'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])
print(df[df['perc'].between(70,90)])

output

Number of student whoes percentage more than 70:and between 70,90


name perc qualify
A Aman 79.5 yes
C Amjad 90.5 yes
J Pooja 89.0 yes

name perc qualify


A Aman 79.5 yes
J Pooja 89.0 yes
PROGRAM 11

Write the code to create a DataFrame ‘df’ and answer the questions followed.

1. Write a command to add one row ‘Chris’ with values 75.6, 98.6, 56.6
2. Write a command to add one column Total = Maths+ Science + SST
3. Write a command to print Score of Maths and Science only.
4. Write a command to update marks of Science of Sudha to 85.0
5. Write a command to delete a row – Mohan

import pandas as pd

data={'Maths':{'Amit':100,'Mohan':95,'Sudha':85},
'Science':{'Amit':100,'Mohan':50,'Sudha':90}, 'SST':{'Amit':60,'Mohan':57.48,'Sudha':53.58}
}

df=pd.DataFrame(data)

print(df)

df.loc['Chris']=[75.6,98.6,56.6]

df['Total']=df['Maths']+df['Science']+df['SST']

print(df[['Maths','Science']])

df.at['Sudha','Science']==85.0 or df.iat[2,1]==85.0

print(df)

df.drop(['Mohan'],inplace=True,axis=0)

print(df)
output

Maths Science SST

Amit 100 100 60.00

Mohan 95 50 57.48

Sudha 85 90 53.58

Maths Science

Amit 100.0 100.0

Mohan 95.0 50.0

Sudha 85.0 90.0

Chris 75.6 98.6

Maths Science SST Total

Amit 100.0 100.0 60.00 260.00

Mohan 95.0 50.0 57.48 202.48

Sudha 85.0 90.0 53.58 228.58

Chris 75.6 98.6 56.60 230.80

Maths Science SST Total

Amit 100.0 100.0 60.00 260.00

Sudha 85.0 90.0 53.58 228.58

Chris 75.6 98.6 56.60 230.80


PROGRAM 12

Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:

Perform the following operations on the DataFrame


1. Create the DataFrame ‘batsman’
2. Add both the scores of a batsman and assign to column “Total”
3. Display the highest score in both Score1 and Score2 of the Data
4. Display the DataFrame.
5. Display the details of PiyushGoel

import pandas as pd

BNo=[1,2,3,4]

Name=['Sunil Pillai','Gaurav Sharma', 'Piyush Goel','Kartik Thakur']

Score1=[90,65,70,80]

Score2=[80,45,90,76]

data={'B_NO':BNo,'NAME':Name,'SCORE1':Score1,'SCORE2':Score2}

batsman=pd.DataFrame(data)

batsman['TOTAL']=batsman['SCORE1']+batsman['SCORE2']

print(batsman)

print('Highest of Score1 : ',batsman['SCORE1'].max())

print('Highest of Score2 : ',batsman['SCORE2'].max())

print(batsman)

print(batsman[batsman['NAME']=='Piyush Goel'])
output
B_NO NAME SCORE1 SCORE2 TOTAL
0 1 Sunil Pillai 90 80 170
1 2 Gaurav Sharma 65 45 110
2 3 Piyush Goel 70 90 160
3 4 Kartik Thakur 80 76 156

Highest of Score1 : 90
Highest of Score2 : 90

B_NO NAME SCORE1 SCORE2 TOTAL


0 1 Sunil Pillai 90 80 170
1 2 Gaurav Sharma 65 45 110
2 3 Piyush Goel 70 90 160
3 4 Kartik Thakur 80 76 156

B_NO NAME SCORE1 SCORE2 TOTAL


2 3 Piyush Goel 70 90 160
PROGRAM 13

import pandas as pd

d={'Icategory':['A','B','A','A','B','C','B','C'],'Iname':['ipad','lcd','iphone','iwatch','projecto
r','harddisk','smartboard','pendrive'],'expenditure':[288000,356000,497000,315000,417
000,45000,211000,21000]}

sales=pd.DataFrame(d,index=['I001','I002','I003','I004','I005','I006','I007','I008'])

print(sales)

print('\n TOTAL EXPENDITURE AFTER GROUPING THE CATEGORIES')

print('**************************************************')
print(sales.groupby('Icategory')['expenditure'].sum())
OUTPUT

Icategory Iname expenditure


I001 A ipad 288000

I002 B lcd 356000

I003 A iphone 497000

I004 A iwatch 315000

I005 B projector 417000

I006 C harddisk 45000

I007 B smartboard 211000


I008 C pendrive 21000
TOTAL EXPENDITURE AFTER GROUPING THE CATEGORIES

*******************************************************

Icategory

A 1100000

B 984000

C 66000

Name: expenditure, dtype: int64

>>>
PROGRAM14

import pandas as pd

import numpy as np

srs=[10,20,30,40,50]

sr1=pd.Series(srs)

print(sr1)

print('\n')

print('AFTER SETTING ALL THE ELEMENTS TO 100')

print('*************************************')

sr1[0:5]=100

print(sr1)

print('\n')

print('NEW SERIES WITH INDEX VALUES FROM A TO E')


print('****************************************')

b=pd.Series(srs,index=['A','B','C','D','E'])

print(b)

print('\n')

print('ADD 10 TO ALL THE ELEMENTS OF THE SERIES')

print('****************************************')

print(b+10)

print('\n')

print('DISPLAY 1ST AND 4TH ELEMENTS OF THE SERIES')

print('******************************************')

print(b[0:4:3])

print('\n')
print('SET THE VALUE OF 3RD ELEMENT TO 500')

print('***********************************')

b[2]=500

print(b)

OUTPUT

0 10

1 20

2 30

3 40

4 50

dtype: int64

AFTER SETTING ALL THE ELEMENTS TO 100

*************************************

0 100

1 100

2 100

3 100

4 100

dtype: int64

NEW SERIES WITH INDEX VALUES FROMA TO E

****************************************

A 10

B 20

C 30

D 40

E 50

dtype: int64
ADD 10 TO ALL THE ELEMENTS OF THE SERIES

****************************************

A 20

B 30

C 40

D 50

E 60

dtype: int64

DISPLAY 1ST AND 4TH ELEMENTS OF THE SERIES

******************************************

A 10

D 40

dtype: int64

SET THE VALUE OF 3RD ELEMENT TO 500

***********************************

A 10

B 20

C 500

D 40

E 50

dtype: int64

>>>
PROGRAM 15

import numpy as n

import pandas as pd

a=[(1,"Sam",24,24,20,22),(2,"Priya",18,17,19,22),

(3,"Lekha",20,22,18,24),(4,"Ram",22,20,24,20),

(5,"Shreya",15,20,18.5,22),(6,"Riya",20,15,22,24)]

df=pd.DataFrame(a,columns=['Rollno','Name','Test1','Test2','Test3','Test4'])

print(df)

print()

print("\tDATA TYPE OF EACH COLUMN OF THE DATAFRAME")

print("\t ")

data_types=df.dtypes

print(data_types)

print()

print("\tCOLUMN LABELS")

print("\t ")

print(df.columns.values)

print()

print("\tROW LABELS")

print("\t -------- ")

print(df.index.values)

print()

print("DIMENSION OF THE DATAFRAME : ", df.ndim)


print("SHAPE OF THE DATAFRAME : ", df.shape)
OUTPUT

Rollno Name Test1 Test2 Test3 Test4

0 1 Sam 24 24 20.0 22

1 2 Priya 18 17 19.0 22

2 3 Lekha 20 22 18.0 24

3 4 Ram 22 20 24.0 20

4 5 Shreya 15 20 18.5 22
5 6 Riya 20 15 22.0 24

DATA TYPE OF EACH COLUMN OF THE DATAFRAME

Rollno int64

Name object

Test1 int64

Test2 int64

Test3 float64

Test4 int64

dtype: object

COLUMN LABELS

['Rollno' 'Name' 'Test1' 'Test2' 'Test3' 'Test4”]

ROW LABELS

[0 1 2 3 4 5]

DIMENSION OF THE DATAFRAME : 2

SHAPE OF THE DATAFRAME : (6, 6)

>>>
PROGRAM 16
import numpy as n

import pandas as pd

a={"Rollno":[1,2,3,4,5],"Name":["Sam","Priya","Lekha","Ram","Shreya"],

"Age":[16,15,16,15,17],"M1":[54,66,40,82,93],"M2":[46,97,49,72,84],

"M3":[80,92,78,84,66], "M4":[56,70,84,92,40],"M5":[55,70,69,52,46]}

df=pd.DataFrame(a)

print(df)

df['Total']=df['M1']+df['M2']+df['M3']+df['M4']+df['M5']

print()

print("\t\tTOTAL MARKS")

print("\t\t --------- ")

print(df)

df.set_index('Rollno',inplace=True)

print()

print("\tCHANGING DEFAULT INDEX TO ROLLNO")


print("\t ")

print(df)

print()

print("\tDISPLAY THE DETAILS OF 1st & 3rd STUDENT")

print("\t ")

print(df[0:3:2])

print()

print("\tREAD RECORDS USING ITERATION")

print("\t ")

for index,values in df.iterrows():

print(index,values)
print()

print("\t\tADD NEW ROW")

print("\t\t --------- ")

data={"Name":"Sree","Age":17,"M1":67,"M2":78,"M3":87,"M4":98,"M5":49}

srs=pd.Series(data,name=6)

df=df.append(srs)

df['Total']=df['M1']+df['M2']+df['M3']+df['M4']+df['M5']

print(df)

print()

print("\tDATAFRAME AFTER DELETING 3rd RECORD")

print("\t ")

df=df.drop(3,axis=0)

print(df)

OUTPUT

Rollno Name Age M1 M2 M3 M4 M5


0 1 Sam 16 54 46 80 56 55

1 2 Priya 15 66 97 92 70 70

2 3 Lekha 16 40 49 78 84 69

3 4 Ram 15 82 72 84 92 52
4 5 Shreya 17 93 84 66 40 46
TOTAL MARKS

Rollno Name Age M1 M2 M3 M4 M5 Total

0 1 Sam 16 54 46 80 56 55 291

1 2 Priya 15 66 97 92 70 70 395

2 3 Lekha 16 40 49 78 84 69 320

3 4 Ram 15 82 72 84 92 52 382
4 5 Shreya 17 93 84 66 40 46 329

CHANGING DEFAULT INDEX TO ROLLNO

Name Age M1 M2 M3 M4 M5 Total

Rollno

1 Sam 16 54 46 80 56 55 291

2 Priya 15 66 97 92 70 70 395

3 Lekha 16 40 49 78 84 69 320

4 Ram 15 82 72 84 92 52 382
5 Shreya 17 93 84 66 40 46 329

DISPLAY THE DETAILS OF 1st & 3rd STUDENT

Name Age M1 M2 M3 M4 M5 Total

Rollno

1 Sam 16 54 46 80 56 55 291
3 Lekha 16 40 49 78 84 69 320
READ RECORDS USING ITERATION

1 Name Sam

Age 16

M1 54

M2 46

M3 80

M4 56

M5 55
Total 291

Name: 1, dtype: object

2 Name Priya

Age 15

M1 66

M2 97

M3 92

M4 70

M5 70

Total 395

Name: 2, dtype: object

3 Name Lekha
Age 16

M1 40

M2 49

M3 78

M4 84
M5 69
Total 320

Name: 3, dtype: object

4 Name Ram

Age 15

M1 82

M2 72

M3 84

M4 92

M5 52

Total 382

Name: 4, dtype: object

5 Name Shreya
Age 17

M1 93

M2 84

M3 66

M4 40

M5 46
Total 329

Name: 5, dtype: object


ADD NEW ROW

Name Age M1 M2 M3 M4 M5 Total

Rollno

1 Sam 16 54 46 80 56 55 291

2 Priya 15 66 97 92 70 70 395

3 Lekha 16 40 49 78 84 69 320

4 Ram 15 82 72 84 92 52 382

5 Shreya 17 93 84 66 40 46 329
6 Sree 17 67 78 87 98 49 379

DATAFRAME AFTER DELETING 3rd RECORD

Name Age M1 M2 M3 M4 M5 Total

Rollno

1 Sam 16 54 46 80 56 55 291

2 Priya 15 66 97 92 70 70 395

4 Ram 15 82 72 84 92 52 382

5 Shreya 17 93 84 66 40 46 329

6 Sree 17 67 78 87 98 49 379
>>>
PROGRAM 17

import pandas as pd

d={'Name':["Ankit","Amit","Aishwarya","Priyanka","Priya","Sreya"],
'Age':[21,19,20,18,17,21],'Stream':["Math","Commerce","Science","Math","Math","S
cience"], 'Percentage':[88,92,95,70,65,78]}

Report=pd.DataFrame(d)

print(Report)

print("\n Details of students whose percentage is greater than 80")

print(" -")

print(Report[Report.Percentage>80])

print("\n Details of students who are either in Science or Commerce Stream")

print(" -")

print(Report[(Report.Stream=='Commerce') | (Report.Stream=='Science')])

print("\n Details of students who are in Maths Stream and Percentage>80")

print(" ")

print(Report[(Report.Stream=="Math") & (Report.Percentage>80)])

print("\n Details after increasing percentage of Maths students by 2")

print(" ")

Report.loc[Report.Stream=='Math','Percentage']+=2

print(Report)

print("\n DataFrame after deleting Age and Stream columns")

print(" ")

print(Report.drop(['Age','Stream'],axis=1))
OUTPUT

Details of students whose percentage is greater than 80

Name Age Stream Percentage

0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Science 95

Details of students who are either in Science or Commerce Stream

Name Age Stream Percentage

1 Amit 19 Commerce 92

2 Aishwarya 20 Science 95
5 Sreya 21 Science 78

Details of students who are in Maths Stream and Percentage>80

Name Age Stream Percentage

0 Ankit 21 Math 88

Details after increasing percentage of Maths students by 2

Name Age Stream Percentage

0 Ankit 21 Math 90

1 Amit 19 Commerce 92

2 Aishwarya 20 Science 95

3 Priyanka 18 Math 72

4 Priya 17 Math 67
5 Sreya 21 Science 78
DataFrame after deleting Age and Stream columns

Name Percentage

0 Ankit 90

1 Amit 92

2 Aishwarya 95

3 Priyanka 72

4 Priya 67

5 Sreya 78
>>>
PROGRAM 18

Write a program to create Three series objects namely Term1 , Term2 and Term3
that stores the marks obtained by 5 students in each term respectively in Chemistry.
The names of the students form the index of each series. Calculate Total marks
obtained by each student by taking a sum of 25% of Term1 , 25% of Term2 & 50%
of Term3. Store the final marks of students in another series object named as Total.
Display the calculated total.

Program:
import pandas as
pdLstname=[]
Lstterm1=[]
Lstterm2=[]
Lstterm3=[]
for i in range (5):
name=input("Enter your name ")
Lstname.append(name)
t1=int(input("Enter the marks scored in chemistry in term 1 out of 100 "))
Lstterm1.append(t1)
t2=int(input("Enter the marks scored in chemistry in term 2 out of 100
"))Lstterm2.append(t2)
t3=int(input("Enter the marks scored in chemistry in term 3 out of 100 "))
Lstterm3.append(t3)
Term1=pd.Series(Lstterm1,Lstname)
Term2=pd.Series(Lstterm2,Lstname)
Term3=pd.Series(Lstterm3,Lstname)
Total=pd.Series(0.25*Term1+0.25*Term2+0.5*Term3)
print(Total)
Output
Enter your name Amruth
Enter the marks scored in chemistry in term 1 out of 100 90
Enter the marks scored in chemistry in term 2 out of 100
100Enter the marks scored in chemistry in term 3 out of
100 100Enter your name Alagu
Enter the marks scored in chemistry in term 1 out of 100
78Enter the marks scored in chemistry in term 2 out of 100
89Enter the marks scored in chemistry in term 3 out of 100
98Enter your name Umesh
Enter the marks scored in chemistry in term 1 out of 100 90
Enter the marks scored in chemistry in term 2 out of 100 50
Enter the marks scored in chemistry in term 3 out of 100 80
Enter your name Yarthisha
Enter the marks scored in chemistry in term 1 out of 100 60

Enter the marks scored in chemistry in term 2 out of 100 76


Enter the marks scored in chemistry in term 3 out of 100 77
Enter your name Isha
Enter the marks scored in chemistry in term 1 out of 100 93

Enter the marks scored in chemistry in term 2 out of 100 92

Enter the marks scored in chemistry in term 3 out of 100 91

Amruth 97.50
Alagu 90.75
Umesh 75.00
Yarthisha 72.50
Isha 91.75
dtype: float64
PROGRAM 19

Object1 Population stores the details of the population in 6 metro cities of India &
Object2 AvgIncome stores the total average income reported in the previous year in
each of these metro cities. Calculate the income per capita for each of these metro
cities and store as Object3 PerCapitaIncome. Display all details

Program

import pandas as

pdL1=[]

L2=[]

L3=[]

for i in range(6):

name=input("Enter the name of metro city ")

L1.append(name)

population=int(input("Enter the population "))

L2.append(population)

income=float(input("Enter the average income "))

L3.append(income)

Object1_Population=pd.Series(L2,L1)

Object2_AvgIncome=pd.Series(L3,L1)

Object3_PerCapitaIncome=(Object2_AvgIncome/Object1_Population)

print("Population with respect to each metro

city,Object1_Population,sep="\n")

print("Total average income with respect to each metro

city",Object2_AvgIncome, sep="\n")

print("Income per capita with respect to each metrocity",Object3_PerCapitaIncome,


sep="\n")
Output

Enter the name of metro city

Chennai Enter the population

12000000

Enter the average income 12450

Enter the name of metro city

Delhi Enter the population

1300000 Enter the average

income 34500

Enter the name of metro city

Mumbai Enter the population

15000000

Enter the average income 10500

Enter the name of metro city

Kolkotta Enter the population

12500000

Enter the average income 4900

Enter the name of metro city

Bangalore Enter the population

3400000

Enter the average income 10000

Enter the name of metro city

Hyderabad Enter the population

9000000
Enter the average income 4600

Population with respect to each metro city

Chennai 12000000

Delhi 1300000
Mumbai 15000000
Kolkotta 12500000

Bangalore 3400000
Hyderabad 9000000

dtype: int64

Total average income with respect to each metro city

Chennai 12450.0
Delhi 34500.0
Mumbai 10500.0
Kolkotta 4900.0

Bangalore 10000.0

Hyderabad 4600.0

dtype: float64

Income per capita with respect to each metro city

Chennai 0.001038

Delhi 0.026538

Mumbai 0.000700
Kolkotta 0.000392
Bangalore 0.002941
Hyderabad 0.000511

dtype: float64

>>>
PROGRAM 20
Create the following DataFrame Sales containing year wise sales figures for
five salespersons in INR. Use the years as column labels, and sales person
names as row labels

2014 2015 2016 2017


Madhu 100.5 12000 20000 50000
Kanaga 150.8 18000 50000 60000
Kamala 200.9 22000 70000 70000
Banu 30000 30000 100000 80000
Dharma 40000 45000 125000 90000
Add the details given below in the dataframe which is sales made during
2018 by all salespersons

2018
Madhu 160000
Kanaga 110000
Kamala 500000
Banu 340000
Dharma 900000
i) Display the sales made by all sales persons in the year 2017.
ii) Display the sales made by Madhu and Banu in the year 2017 and 2018.
iii) Display the sales made by Dharma in 2016.
iv) Add data to sales for sales person Karna where the sales made
are [196.2, 37800, 52000, 78438, 38852] in the years [2014,
2015, 2016, 2017, 2018] respectively.
v) Delete the data for the year 2014 from the DataFrame sales.
vi) Change the name of the sales person Kanaga to Kishore, Kamala
to Gridharand Banu to Badri
vii) Update the sales made by Badri in 2018 to 100000
import pandas as pd

Sales=pd.DataFrame([[100.5,12000,20000,50000],[150.8,18000,50000,60000],

[200.9,22000,70000,70000],[30000,30000,100000,80000],[40000,45000,125000,9000
0]],index=["Madhu","Kanaga","Kamala","Banu","Dharma"],columns=[2014,2015,20
16,2017])

Sales[2018]=[160000,110000,500000,340000,900000]

print("Sales made by all sales persons in 2017\n", Sales[2017])

print("Sales made by Madhu and Banu in 2017 and 2018\n",


Sales.loc[["Madhu","Banu"],[2017,2018]])

print("Sales made by Dharma in 2016",Sales.loc["Dharma",2016])

Sales.loc["Karna"]=[196.2,37800,52000,78438,38852]

print("Dataframe after adding details of Karna (a row) \n",Sales)

Sales=Sales.drop(2014,axis=1)

print("Dataframe after deleting details of 2014 (a column) \n",Sales)

Sales=Sales.rename({"Kanaga":"Kishore","Kamala":"Gridhar","Banu":"Badri"},axis=
0)

print("Dataframe after renaming 3 sales person names (row rename) \n",Sales)

Sales.loc["Badri",2018]=100000

print("Dataframe after updating sales made by Badri in 2018 \n",Sales)


OUTPUT

Sales made by all sales persons in 2017

Madhu 50000

Kanaga 60000

Kamala 70000

Banu 80000
Dharma 90000

Name: 2017, dtype: int64

Sales made by Madhu and Banu in 2017 and 2018

2017 2018

Madhu 50000 160000

Banu 80000 340000

Sales made by Dharma in 2016 125000

Dataframe after adding details of Karna (a row)

2014 2015 2016 2017 2018


Madhu 100.5 12000.0 20000.0 50000.0 160000.0

Kanaga 150.8 18000.0 50000.0 60000.0 110000.0


Kamala 200.9 22000.0 70000.0 70000.0 500000.0

Banu 30000.0 30000.0 100000.0 80000.0 340000.0

Dharma 40000.0 45000.0 125000.0 90000.0 900000.0


Karna 196.2 37800.0 52000.0 78438.0 38852.0
Dataframe after deleting details of 2014 (a column)

2015 2016 2017 2018

Madhu 12000.0 20000.0 50000.0 160000.0

Kanaga 18000.0 50000.0 60000.0 110000.0


Kamala 22000.0 70000.0 70000.0 500000.0

Banu 30000.0 100000.0 80000.0 340000.0

Dharma 45000.0 125000.0 90000.0 900000.0

Karna 37800.0 52000.0 78438.0 38852.0

Dataframe after renaming 3 sales personnames (row rename)

2015 2016 2017 2018


Madhu 12000.0 20000.0 50000.0 160000.0

Kishore 18000.0 50000.0 60000.0 110000.0


Gridhar 22000.0 70000.0 70000.0 500000.0

Badri 30000.0 100000.0 80000.0 340000.0

Dharma 45000.0 125000.0 90000.0 900000.0

Karna 37800.0 52000.0 78438.0 38852.0

Dataframe after updating sales made by Badri in 2018

2015 2016 2017 2018


Madhu 12000.0 20000.0 50000.0 160000.0

Kishore 18000.0 50000.0 60000.0 110000.0


Gridhar 22000.0 70000.0 70000.0 500000.0

Badri 30000.0 100000.0 80000.0 100000.0

Dharma 45000.0 125000.0 90000.0 900000.0

Karna 37800.0 52000.0 78438.0 38852.0

>>>
PROGRAM 21
Create a Data Frame quarterly_sales where each row contains the item
category, itemname, make year and expenditure.

ITEM_CATEGORY ITEM_NAME Make year ITEM_EXPENDITURE


CAR FORD 2020 700000
AC HITACHI 2021 50000
AIRCOOLER SYMPHONY 2019 12000
WASHING LG 2021 19000
MACHINE
a) Write the values of DataFrameQuarterly_Sales to a comma separated

file SalesFigures.csv on the disk.


b) Open SalesFigures.csv and check whether all details are correct.

import pandas as pd

C1=["CAR","AC","AIRCOOLER","WASHING MACHINE"]

C2=["FORD","HITACHI","SYMPHONY","LG"]

C3=[2020,2021,2019,2021]

C4=[700000,50000,12000,19000]

quarterly_sales=pd.DataFrame({"ITEM_CATEGORY":C1,"ITEM_NAME":C2,"Ma
ke year":C3,"ITEM_EXPENDITURE":C4})

print(quarterly_sales)

quarterly_sales.to_csv("C:\SalesFigures.csv",index=False)
OUTPUT
PROGRAM 22

A dictionary grade contains the following data:

Grade={‘Name’:[‘Rashmi’,’Harsh’,’Ganesh’,’Priya’,’Vivek’,’Anita’,’Kartik’],

’Grade’:[‘a1’,’a2’,’b1’,’a1’,b2’,’a2’,’a1’]}

Write statements for the following

i)Create dataframe Gr

ii) Find the output of Gr.iloc[0:5] and Gr[0:5]

iii) Add a column called Percentage with the following data:

[92,89,None,95,68,None,93]

iv) Rearrange the columns as Name,Percentage and Grade

v)Drop the column ‘name’

vi)Delete the 3rd and 5th rows

import pandas as pd

Grade={'Name':['Rashmi','Harsh','Ganesh','Priya','Vivek','Anita','Kartik'],'Grade':['a1','
a2','b1','a1','b2','a2','a1']}

Gr = pd.DataFrame(Grade)

print(Gr)

print(Gr.iloc[0:5])

print(Gr[0:5])

Gr['Percentage']=[92,89,None,95,68,None,93]

print(Gr)

print(Gr.iloc[:,[0,2,1]])

Gr=Gr.drop(['Name'],axis=1)

Gr=Gr.drop([2,4],axis=0)

print(Gr)
OUTPUT
PROGRAM 23
Read the ‘Student_result.csv’ to create data frame and do the following operation:

a duplicate file for ‘student_result.csv’ containing AdmNo,


1. To create
Name andPercentage.

2. Writethe statement in Pandas to find the highest percentage and also print
thestudent’s name and percentage.
Program

import pandas as pd

import csv

# To create a duplicate file for ‘student_result.csv’ containing Adm_No,


Name andPercentage.

Df=pd.read_csv("C:\\student_result.csv",sep=",")

print(df)

df.to_csv('C:\\copyStudent_result.csv',columns=['Admno',"Name","Perc

"]) # Display Copied Dataframe

df2=pd.read_csv("C:\\copyStudent_result.csv")

print(df2)

# find the highest percentage and also print the student’s name and

percentage.df1 = pd.read_csv("C:\\student_result.csv")

df1 = df1[["Name",'Perc']][df1.Perc== df1['Perc'].max()]


print(df1)
OUTPUT
PROGRAM 24

import matplotlib.pyplot as plt

Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]

Unemployment_Rate = [9.8, 12, 8, 7.2, 8, 7, 6.5, 6.2, 5.5, 6.3]

plt.xlabel("Year")

plt.ylabel("Unemployment_Rate")

plt.title("Unemployment_Rate from 1920 - 2010")

plt.plot(Year,Unemployment_Rate,'g',linewidth=5,linestyle='dashed')

plt.show()

OUTPUT
PROGRAM 25

import matplotlib.pyplot as plt

objects=["Python", "C++", "Perl", "Java", "VB"]

performance = [10,8,6,3,1]

plt.xlabel("Programming Languages")

plt.ylabel("Performance")

plt.title("RATING OF DIFFERENT PROGRAMMING LANGUAGES")

plt.bar(objects,performance,width=0.9,color=['r','b','y','g'])

plt.show()

OUTPUT
PROGRAM 26

import numpy as np

import matplotlib.pyplot as plt

x=np.random.randint(5,50,30)

plt.hist(x,bins=10,edgecolor='r',facecolor='y')

plt.show()

OUTPUT
PROGRAM 27

import pandas as pd

import matplotlib.pyplot as plt

dict1={"Name":["Anju","Rishi","Riya","Priya","Shiva"],

"Total":[543,497,398,589,534]}

df1=pd.DataFrame(dict1,index=["Anju","Rishi","Riya","Priya","Shiva"])

df1.plot.bar(color='g') # OR --> df1.plot(kind="bar")

plt.show()

OUTPUT
PROGRAM 28

import matplotlib.pyplot as pl

Subject=['Maths','Phy.','Chem.','Bio.','C.Sc.','English','Tamil','Hindi']

Class=['XI','XII']

Sub_Percentage=[86,84,78,86,94,87,90,88]

Class_Percentage=[90,100]

pl.bar(Subject,Sub_Percentage,align='center')

pl.bar(Class,Class_Percentage)

pl.xlabel('Subject & Class Names', fontsize=18)

pl.ylabel('Pass Percentage', fontsize=18)

pl.title('Student Result Analysis',fontsize=22)

pl.show()

OUTPUT
PROGRAM 29

'''Given the following data of rainfall in North & South zones of India in mm for
12 months.

Create multiple Bar charts in a Figure to compare rail fall of North and South
zone from Jan to Dec.'''

import matplotlib.pyplot as pl

import numpy as np

months =
['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']

north = [14,13,13,19,16,20,15,17,19,17,15,12]

south= [16,20,13,20,20,17,11,16,13,14,17,20]

index=np.arange(len(months))

pl.bar(index-0.125,north,color='r',width=0.25,label = 'north')

pl.bar(index+0.125,south,color='y',width=0.25,label='south')

pl.xticks(index,months)

pl.xlabel('month-->',fontsize=15,color='green')

pl.xlabel('rainfall (in mm)-->', fontsize=10, color='green')

pl.title('rainfall analysis\n of north & south zone', color ='pink')

pl.grid()

pl.legend()

pl.show()
OUTPUT
PROGRAM 30

Write a Python program to display the Result using a LINE PLOT

1. Set the title of graph is “Result Analysis”


2. Display the label of x axis to “Names” and y axis to “Score”
3. Display the legends.

import matplotlib.pyplot as plt


Names = ['Amit', 'Mohan', 'Sudha']
Maths = [100, 90, 80]
Science = [90, 50, 70]
SST = [60, 80, 70]
plt.plot(Names,Maths, color='red', linestyle='dashdot', marker='*', linewidth=1.5,
label='Mat')
plt.plot(Names,Science, color='blue', ls='dashdot', marker='o', linewidth=1.5, label='Sci')
plt.plot(Names,SST, color='green', ls='dashdot', marker='D', linewidth=1.5, label='SST')
plt.title('Result Analysis', fontsize=15)
plt.xlabel('Names')
plt.ylabel('Score')
plt.legend()
plt.show()
output
PROGRAM 31

Write a python program to generate line graph with suitable title and labels.
Where x is the year of performance with values 2014,2015,2016,2017,2018 and 2019.
And y axis shows the profit of a particular company in Rs.(Millions).

import matplotlib.pyplot as plt

x1=[]

for i in range(2014,2020):

x1.append(i)

y1=[10000,5000,20000,17000,12000,7000]

plt.plot(x1,y1,color='r',marker='^',

markeredgecolor='b', linestyle='dashed')

plt.grid()

plt.xlabel("Year ->", color='blue')

plt.ylabel("Profit Rs. (in Millions)->",color='blue')

plt.title("Metcalf and Hodkingson Pvt.\Ltd.\n Yearly Profit Analysis",color='r')

plt.show()
output
PROGRAM 32

Given the following data of rainfall in different zones of India in mm for 12


months, Create multiple lines chart in a Figure to observe any trends fromJan to
Dec.
Zones Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
North 14 13 13 19 16 20 15 17 19 17 15 12
South 16 20 13 20 20 17 11 16 13 14 17 20

import matplotlib.pyplot as plt

label = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

y1 =[14,13,13,19,16,20,15,17,19,17,15,12]

y2=[16,20,13,20,20,17,11,16,13,14,17,20]

plt.plot(label, y1,linestyle='dashed',label='North

Zone',marker='s',markeredgecolor='r')

plt.plot(label, y2,label='South Zone',marker='^',markeredgecolor='b')

plt.xlabel('Month->', fontsize=10, color='red')

plt.ylabel('Rainfall ( in mm)->', fontsize=10, color='red')

plt.title('ZONE WISE RAINFALL ANALYSIS', color='blue')

plt.grid()

plt.legend()

plt.show()
OUTPUT
PROGRAM 33

Given the ages of 80 participants in some game. Write a program toplot a

histogram from given data with 6 bins of your choice.

Data = [ 9,10,11,13,13,15,16,17,18,19,21,23,23,23,24,24,25,25,25,25,25,
26,26,27,27,27, 27, 29,30,30,30,30,31,33,34,34,35,36, 36,37,37,37,
38,39,40,40,40,41,42,43,43,39,30,31,32,33,34,35,36,37,38,39,36,37,38,
40,41,42,43,44,45,50,51,52,53,54,55,56,57,58]

import matplotlib.pyplot as plt

data=[ 9,10,11,13,13,15,16,17,18,19,21,23,23,23,24,24,25,

25,25,25,25, 26,26,27,27,27, 27, 29,30,30,30,30,31,

33,34,34,35,36, 36,37,37,37, 38,39,40,40,40,41,42,43,

43,39,30,31,32,33,34,35,36,37,38,39,36,37,38,

40,41,42,43,44,45,50,51,52,53,54,55,56,57,58]

b=[0,10,20,30,40,50,60]

plt.hist(data,bins=b,color='g',edgecolor='b')

plt.title("Participants of Game in different Age Groups",

color='r', fontsize=16)

plt.xlabel("Age interval -->",fontsize=14, color='b')

plt.ylabel("No. of Participants -->",fontsize=14, color='b')

plt.show()
OUTPUT
PROGRAM 34

import pandas as pd

d={'Name':['Ajay','Vinay','Sonia','Dileep','Radhika','Shaurya','Boby','Anjana','Ri
nku'],
'Age':[26,24,23,22,23,24,21,26,22],'Score':[62,63,55,74,31,83,77,85,31]}

df=pd.DataFrame(d)

df.to_csv('C:\\Users\\ELCOT\Desktop\\NEW.csv',index=None)

import pandas as pd

df1=pd.read_csv('C:\\Users\\ELCOT\\Desktop\\NEW.csv')

print(df1)

OUTPUT

Name Age Score


0 Ajay 26 62

1 Vinay 24 63

2 Sonia 23 55

3 Dileep 22 74

4 Radhika 23 31

5 Shaurya 24 83

6 Boby 21 77

7 Anjana 26 85

8 Rinku 22 31
>>>
PROGRAM 35

import csv

f=open("empxyz.csv","a",newline='')

w=csv.writer(f)

l=["ENO","NAME","AGE","CITY","SALARY"]

w.writerow(l)

n=int(input("Enter no. of employees ="))

for i in range(n):

r=int(input("Enter emp no="))

name=input("Enter name=")

age=int(input("Enter Age="))

city=input("Enter city=")

salary=int(input("Enter Salary="))

l=[r,name,age,city,salary]

w.writerow(l)

f.close()

f=open("empxyz.csv","r")

st=csv.reader(f)

for i in st:

print(i)

f.close()
OUTPUT:

Enter no. of employees =5

Enter emp no=100

Enter name=Aliya

Enter Age=15

Enter city=chennai

Enter Salary=25000

Enter emp no=101

Enter name=Syed

Enter Age=16

Enter city=delhi

Enter Salary=35000

Enter emp no=103

Enter name=mateen

Enter Age=15

Enter city=ooty

Enter Salary=25000

Enter emp no=104

Enter name=Meena

Enter Age=17

Enter city=kodai

Enter Salary=27000

Enter emp no=105

Enter name=fahima
Enter Age=16

Enter city=chennai

Enter Salary=29000

['ENO', 'NAME', 'AGE', 'CITY', 'SALARY']

['100', 'Aliya', '15', 'chennai', '25000']

['101', 'Syed', '16', 'delhi', '35000']

['103', 'mateen', '15', 'ooty', '25000']

['104', 'Meena', '17', 'kodai', '27000']

['105', 'fahima', '16', 'chennai', '29000']

>>>
PROGRAM 36

import csv

f=open(r"empxyz.csv","r")

csvr=csv.reader(f)

found=0

eno=input('enter employee no.:')

for r in csvr:

if (r[0]==eno):

print(r)

found=1

f.close()

if found==0:

print('record not found')

else:

print('Record searched successfully')

f.close()

OUTPUT:

enter employee no.:104

['104', 'Meena', '17', 'kodai', '27000']

Record searched successfully


PROGRAM 37

import csv

f=open(r"empxyz.csv","r")

csvr=csv.reader(f)

found=0

li=[]

eno=input('enter employee no to delete:')

for r in csvr:

if (r[0]!=eno):

li.append(r)

else:
found=1

f.close()

if found==0:

print('record not found')

else:

f=open("empxyz.csv","w", newline='')

csvr=csv.writer(f)

csvr.writerows(li)

print('Record deleted successfully')

f.close()
OUTPUT:

enter employee no to delete:103

Record deleted successfully

>>>
PROGRAM 38

Create a data frame marks using a dictionary with name column as the index.
Marks in TEST1and TEST2 are taken as input from the user.

NAME TEST1 TEST2


KARTHIK 42 41
JOEL 26 33
AMRUTH 36 44
UMESH 35 39
ARUNA 49 47

a. Add both TEST1 and TEST2 marks of each student and create a column
“Total”
b. Add a new student detail to the data frame as
given belowTARUN,30,29
c. Take a backup of the data frame using a csv file Marks.csv .
d. Import the data from Marks.csv and plot student vs total marks obtained
using a bargraph.
i. The X-axis should have the student’s names as its labels.
ii. The graph should have proper title and axes titles.
iii. Display the grid and choice of colour of the bar is orange

import pandas as pd
import matplotlib.pyplot as plt
lst1=[]
lst2=[]
name=["KARTHIK","JOEL","AMRUTH","UMESH","ARUNA"]
for i in name:
print("enter the marks of ",i," in the first test ")
a=float(input())
lst1.append(a)
print("enter the marks of ",i," in the second test ")
b=float(input())
lst2.append(b)
dic1={"TEST1":lst1,"TEST2":lst2}
marks=pd.DataFrame(dic1,index=name)
marks.index.name="NAME"
print(marks)
marks["TOTAL"]=marks["TEST1"]+marks["TEST2"]
print(marks)
marks.loc["TARUN"]=[30,29,59]
marks.to_csv("C:/CLASS XI & XII/marks.csv")
marks1=pd.read_csv("C:\CLASS XI & XII/marks.csv")
print(marks1)
marks1.plot(kind="bar",x="NAME",title="performancereport",color="orange")
plt.ylabel("marks")
plt.grid(True)
plt.show()

Output
enter the marks of KARTHIK in the first test
42
enter the marks of KARTHIK in the second test
41
enter the marks of JOEL in the first test
26
enter the marks of JOEL in the second test
33
enter the marks of AMRUTH in the first test
36
enter the marks of AMRUTH in the second test
44
enter the marks of UMESH in the first test
35
enter the marks of UMESH in the second test
39
enter the marks of ARUNA in the first test
49
enter the marks of ARUNA in the second test
47
TEST1 TEST2
NAME
KARTHIK 42.0 41.0
JOEL 26.0 33.0
AMRUTH 36.0 44.0
UMESH 35.0 39.0
ARUNA 49.0 47.0

TEST1 TEST2 TOTAL


NAME
KARTHIK 42.0 41.0 83.0
JOEL 26.0 33.0 59.0
AMRUTH 36.0 44.0 80.0
UMESH 35.0 39.0 74.0
ARUNA 49.0 47.0 96.0
NAME TEST1 TEST2 TOTAL
0 KARTHIK 42.0 41.0 83.0
1 JOEL 26.0 33.0 59.0
2 AMRUTH 36.0 44.0 80.0
3 UMESH 35.0 39.0 74.0
4 ARUNA 49.0 47.0 96.0
5 TARUN 30.0 29.0 59.0
PROGRAM 39
1. WRITE A PYTHON PROGRAM TO CREATE A LIST, DICTONIARY USING
DATAFRAME AND MATPLOTLIB TO ODRERD THE ITEMS

import pandas as pd

importmatplotlib.pyplot as plt

l1=[]

l2=[]

l3=[]

l4=[]

l5=[]

fori in range(5):

a=input('Enter orderid')

b=input('Enter ordername')

c=int(input('Enter price'))

d=int(input('Enter delivery charge'))

e=input('Enter location')

l1.append(a)

l2.append(b)

l3.append(c)

l4.append(d)

l5.append(e)

d1={'orderid':l1,'ordername':l2,'price':l3,'Delivery charges':l4,'location':l5}

order=pd.DataFrame(d1)

a=order[order['Delivery charges']>30]

print(a)

order['Total']=order['price']+order['Delivery charges']

print(order.loc[[0,2,4],:])
order.to_csv('Ite.csv')

item=pd.read_csv('Ite.csv')

item.plot(kind='bar',x='ordername',y='price',color='red',edgecolor='yellow')

plt.xlabel("Order Name")

plt.ylabel("Price")

plt.title("Order Details")

plt.show()

OUTPUT

Enter orderid1

Enter ordernameAPPLE

Enter price45

Enter delivery charge12

Enter locationCHENNAI

Enter orderid2

Enter ordernameORANGE

Enter price67

Enter delivery charge15

Enter locationOOTY

Enter orderid3

Enter ordernameGRAPES

Enter price45

Enter delivery charge14

Enter locationKODAI

Enter orderid4

Enter ordernameBADAM

Enter price600
Enter delivery charge34

Enter locationDELHI

Enter orderid5

Enter ordernameFIGS

Enter price1000

Enter delivery charge45

Enter locationCHENNAI

orderid ordername price Delivery charges location

3 4 BADAM 600 34 DELHI

4 5 FIGS 1000 45 CHENNAI

Ordered ordername price Delivery charges location Total

0 1 APPLE 45 12 CHENNAI 57
2 3 GRAPES 45 14 KODAI 59
4 5 FIGS 1000 45 CHENNAI 1045
PROGRAM 40
WRITE A PYTHON PROGRAM TO CREATE A LIST, DICTONIARY USING SERIES ,
MATPLOTLIB AND CSV FILE TO ENTER THE BOOKS DETAILS .

import pandas as pd

importmatplotlib.pyplot as plt

l1=[]

l2=[]

l3=[]

l4=[]

l5=[]

fori in range(5):

a=input('Enter book ID')

b=input('enter subject')

c=input('Enter Booktitle')

d=input('Enter class in roman numerical')

e=int(input('Enter price'))

l1.append(a)

l2.append(b)

l3.append(c)

l4.append(d)

l5.append(e)

s1=pd.Series(l1)

s2=pd.Series(l2)

s3=pd.Series(l3)

s4=pd.Series(l4)

s5=pd.Series(l5)

d1={'BookID':s1,'Subject':s2,'BookTitle':s3,'Class':s4,'Price':s5}

Books=pd.DataFrame(d1)
print(Books)

print(Books[Books['Class']=='XII'])

a=Books[Books['Price']>250]

print(a)

Books.loc[5]=['B0006','Artificial Intelligence','Machine Learning','XI',350]

print(Books)

Books.to_csv('Book.csv')

Books1=pd.read_csv('Book.csv')

print(Books1)

a=Books1['BookID']

b=Books1['Price']

plt.plot(a,b,color='blue')

plt.title("Book details")

plt.show()

OUTPUT

Enter book ID100

entersubjectCOMPUTER SCIENCE

Enter BooktitlePYTHON

Enter class in roman numericalXII

Enter price340

Enter book ID101

entersubjectACCOUNTANCY

Enter BooktitleIYER

Enter class in roman numericalXII

Enter price700

Enter book ID102

entersubjectPYTHON
Enter BooktitlePANDAS

Enter class in roman numericalXI

Enter price800

Enter book ID103

entersubjectENGLISH

Enter BooktitleFIRE

Enter class in roman numericalXII

Enter price550

Enter book ID104

entersubjectMATHS

Enter BooktitleTRIANGLE

Enter class in roman numericalXI

Enter price900

BookID Subject BookTitle Class Price

0 100 COMPUTER SCIENCE PYTHON XII 340

1 101 ACCOUNTANCY IYER XII 700

2 102 PYTHON PANDAS XI 800

3 103 ENGLISH FIRE XII 550


4 104 MATHS TRIANGLE XI 900

BookID Subject BookTitle Class Price


0 100 COMPUTER SCIENCE PYTHON XII 340

1 101 ACCOUNTANCY IYER XII 700


3 103 ENGLISH FIRE XII 550
BookID Subject BookTitle Class Price
0 100 COMPUTER SCIENCE PYTHON XII 340

1 101 ACCOUNTANCY IYER XII 700

2 102 PYTHON PANDAS XI 800

3 103 ENGLISH FIRE XII 550


4 104 MATHS TRIANGLE XI 900

BookID Subject BookTitle Class Price

0 100 COMPUTER SCIENCE PYTHON XII 340


1 101 ACCOUNTANCY IYER XII 700

2 102 PYTHON PANDAS XI 800

3 103 ENGLISH FIRE XII 550

4 104 MATHS TRIANGLE XI 900


5 B0006 Artificial Intelligence Machine Learning XI 350

Unnamed: 0 BookID Subject BookTitle Class Price

0 0 100 COMPUTER SCIENCE PYTHON XII 340


1 1 101 ACCOUNTANCY IYER XII 700

2 2 102 PYTHON PANDAS XI 800

3 3 103 ENGLISH FIRE XII 550

4 4 104 MATHS TRIANGLE XI 900


5 5 B0006 Artificial Intelligence Machine Learning XI 350
PROGRAM 41

WRITE A PYTHON PROGRAM TO CREATE A LIST, DICTONIARY USING


DATAFRAME AND MATPLOTLIB TO VIEW THE BIRTH RATE AND
POPULATIONOF EVERY COUNTRY

import pandas as pd

importmatplotlib.pyplot as plt

l1=[]

l2=[]

l3=[]

l4=[]

d={}

for i in range(3):

a=input('Enter Country Name')

b=float(input('Enter

population'))

c=float(input('Enter Birth

Rate'))d=input('Enter Date')

l1.append

(a)

l2.append

(b)

l3.append(c

)
l4.append(d

d={'country':l1,'population':l2,'birthrate':l3,'updatedate':l4}

country=pd.DataFrame(d)

print(country)

print(country.iloc[0:2,:])
print(country[country['birthrate']>1

9])

country.loc[5]=['Pakistan',194754000,27.62,'2010-04-

05']print(country)

country.to_csv('country.csv')

country1=pd.read_csv('country.csv')
country1.plot(x='country',y='population',kind='bar',color='green')

plt.title("BirthRate Analysis")

plt.show()

OUTPUT

Enter Country NameCHENNAI

Enter population23456543

Enter Birth Rate100

Enter Date12/10/2022

Enter Country NameCHINA

Enter population4566432

Enter Birth Rate100

Enter Date10/09/2021

Enter Country NamePAKISTAN

Enter population453232

Enter Birth Rate95

Enter Date23/05/2022

country population birthrate updatedate


0 CHENNAI 23456543.0 100.0 12/10/2022

1 CHINA 4566432.0 100.0 10/09/2021


2 PAKISTAN 453232.0 95.0 23/05/2022
country population birthrate updatedate
0 CHENNAI 23456543.0 100.0 12/10/2022

1 CHINA 4566432.0 100.0 10/09/2021

country population birthrate updatedate

0 CHENNAI 23456543.0 100.0 12/10/2022

1 CHINA 4566432.0 100.0 10/09/2021

2 PAKISTAN 453232.0 95.0 23/05/2022

country population birthrate updatedate

0 CHENNAI 23456543.0 100.00 12/10/2022

1 CHINA 4566432.0 100.00 10/09/2021

2 PAKISTAN 453232.0 95.00 23/05/2022


5 Pakistan 194754000.0 27.62 2010-04-05
PROGRAM 42

The cricket scores of Indian and Australian teams in a one day match after 10,20,30,40 and
50over are recorded in a Dataframe. The data should be collected from the user. Using pyplot
method represents the data using line graph. Customize the graph.

import pandas as pd

importmatplotlib.pyplot as plt

il=[]

al=[]

for 0 in range(10,51,10):

print('enter runs scored by india after ',o,'overs')

i=int(input())

il.append(i)

print('enter runs scored by australia after ',o,'overs')

a=int(input())

al.append(a)

plt.plot(range(10,51,10),il,marker='*',markersize=10)

plt.plot(range(10,51,10),al,marker='.',markersize=10)

plt.title('Comparision of runs of India and Australia')

plt.xlabel('Overs')

plt.ylabel('Runs')

plt.legend(['India','Australia'])

plt.show()
OUTPUT

enter runs scored by india after 10 overs

200

enter runs scored by australia after 10 overs

250

enter runs scored by india after 20 overs

350

enter runs scored by australia after 20 overs

299

enter runs scored by india after 30 overs

399

enter runs scored by australia after 30 overs

450

enter runs scored by india after 40 overs

450

enter runs scored by australia after 40 overs

399

enter runs scored by india after 50 overs

465

enter runs scored by australia after 50 overs

470
PROGRAM 43

Create a data frame marks using a dictionary with name column as the index. Marks in TEST1
and TEST2 are taken as input from the user.

NAME TEST1 TEST2


KARTHIK 98 99
ANAS 100 99
AFRIN 89 95
SALMAN 98 95
AIYESHA 100 100
a. Add both TEST1 and TEST2 marks of each student and create a column “Total”
b. Add a new student detail to the data frame as given belowTARUN,30,29
c. Take a backup of the data frame using a csv file Marks.csv .
d. Import the data from Marks.csv and plot student vs total marks obtained using a bar graph.
i. The X-axis should have the student’s names as its labels.
ii. The graph should have proper title and axes titles.
iii. Display the grid and choice of colour of the bar is orange

import pandas as pd

importmatplotlib.pyplot as plt

lst1=[]

lst2=[]

name=["KARTHIK","ANAS","AFRIN","SALMAN","AIYESHA"]

fori in name:

print("enter the marks of ",i," in the first test ")

a=float(input())

lst1.append(a)

print("enter the marks of ",i," in the second test ")

b=float(input())

lst2.append(b)

dic1={"TEST1":lst1,"TEST2":lst2}

marks=pd.DataFrame(dic1,index=name)

marks.index.name="NAME"

print(marks)
marks["TOTAL"]=marks["TEST1"]+marks["TEST2"]

print(marks)

marks.loc["TARUN"]=[30,29,59]

marks.to_csv("ma.csv")

marks1=pd.read_csv("ma.csv")

print(marks1)

marks1.plot(kind="bar",x="NAME",title="performancereport",color="orange")

plt.ylabel("marks")

plt.grid(True)

plt.show()

OUTPUT

enter the marks of KARTHIK in the first test

98

enter the marks of KARTHIK in the second test

99

enter the marks of ANAS in the first test

100

enter the marks of ANAS in the second test

99

enter the marks of AFRIN in the first test

89

enter the marks of AFRIN in the second test

95

enter the marks of SALMAN in the first test

98

enter the marks of SALMAN in the second test

95
enter the marks of AIYESHA in the first test

100

enter the marks of AIYESHA in the second test

100

TEST1 TEST2
NAME

KARTHIK 98.0 99.0

ANAS 100.0 99.0

AFRIN 89.0 95.0

SALMAN 98.0 95.0

AIYESHA 100.0 100.0

TEST1 TEST2 TOTAL

NAME

KARTHIK 98.0 99.0 197.0

ANAS 100.0 99.0 199.0

AFRIN 89.0 95.0 184.0

SALMAN 98.0 95.0 193.0

AIYESHA 100.0 100.0 200.0

NAME TEST1 TEST2 TOTAL

0 KARTHIK 98.0 99.0 197.0

1 ANAS 100.0 99.0 199.0

2 AFRIN 89.0 95.0 184.0

3 SALMAN 98.0 95.0 193.0

4 AIYESHA 100.0 100.0 200.0


5 TARUN 30.0 29.0 59.0
PROGRAM 44

WRITE A PYTHON PROGRAM TO CREATE A DICTONIARY USING DATAFRAME


AND MATPLOTLIB TO VIEW THE NAME AND TOTAL OF EACH STUDENT

import pandas as pd

import matplotlib.pyplot as plt

dict1={"Name":["Anju","Rishi","Riya","Priya","Shiva"], "Total":[543,497,398,589,534]}
df1=pd.DataFrame(dict1,index=["Anju","Rishi","Riya","Priya","Shiva"])

df1.plot.bar(color='g')

plt.show()

OUTPUT
PROGRAM 45

The following data is stored in collection.csv. Import the data in a dataframe

Represent the collection of different counters on different days using vertical bar chart and
horizontal bar chart. X axis should be days and y axis should be sales made. Customize the
charts to make them more meaningful.

import pandas as pd
import matplotlib.pyplot as plt
collection=pd.read_csv("C:/collection.csv")
print(collection)
collection.plot(kind="bar",x="days",title="salesreport",color=["red"," green","purple"])
plt.ylabel("sales")
plt.show()
collection.plot(kind="barh",x="days",title="salesreport",color=["blue","black","brown"])
plt.xlabel("sales")
plt.show()

OUTPUT

COUNTER -1 COUNTER-2 COUNTER-3 DAYS

0 8000 8999 12000 SUNDAY

1 50000 90000 45000 MONDAY

2 900000 76544 3400 TUESDAY

3 57888 12000 3487 WEDNESDAY

4 78655 14000 233452 FRIDAY

5 899000 17000 4500 SATURDAY


PROGRAM 46

Take Data of your interest from an open source (e.g. data.gov.in), aggregate and
summarize it. Then plot it using different plotting functions of the Matplotlib Library.

import pandas as pd

import matplotlib.pyplot as pl

cf=pd.read_csv("PEDV.csv")

print(cf,'\n')

s=(cf['Team'].head(12))

s2=(cf['Salary'].head(12))

pl.bar(s,s2)

pl.xlabel('Name of the Team', fontsize=18)

pl.ylabel('Salary Range', fontsize=18)

pl.title('Aggregated and Summarized Data',fontsize=22)

pl.legend('Salary', loc='best')

pl.show()
output

You might also like