Xii Ip Practical File 24-25
Xii Ip Practical File 24-25
Xii Ip Practical File 24-25
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
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
elements'))dict1={}
for i in range(n):
key=input('entername')
dict1[key]=value
print('new dictionary')
print('**************')
print(dict1)
srs=pd.Series(dict1)print()
print('**********************')
print(srs)print()
l1=[]
for j in range(n1):
l1.append(ele)
print('new list')
print('********')
print(l1)
import numpy as n
arr=n.array(l1)
print()
print('*********************')
srs1=pd.Series(arr)
print(srs1)
OUTPUT
new dictionary
**************
***********************
MATEEN 460
HASAN 355
ZIA 480
dtype: int64
enter the number of elements 3
new list
********
*********************
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('**********************')
print(srs.head(3))
print()
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
**********************
andhrapradesh 535
karnataka 568
tamilnadu 897
dtype: int64
********************
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('*************************')
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]}
print(RESULT)
print(RESULT)
print(RESULT[['Maths', 'Science']])
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
T1 100.0 100.0
T2 95.8 100.0
T3 NaN 100.0
T4 82.0 85.4
T5 75.6 98.6
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
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
Mohan 95 50 57.48
Sudha 85 90 53.58
Maths Science
Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:
import pandas as pd
BNo=[1,2,3,4]
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(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
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('**************************************************')
print(sales.groupby('Icategory')['expenditure'].sum())
OUTPUT
*******************************************************
Icategory
A 1100000
B 984000
C 66000
>>>
PROGRAM14
import pandas as pd
import numpy as np
srs=[10,20,30,40,50]
sr1=pd.Series(srs)
print(sr1)
print('\n')
print('*************************************')
sr1[0:5]=100
print(sr1)
print('\n')
b=pd.Series(srs,index=['A','B','C','D','E'])
print(b)
print('\n')
print('****************************************')
print(b+10)
print('\n')
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
*************************************
0 100
1 100
2 100
3 100
4 100
dtype: int64
****************************************
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
******************************************
A 10
D 40
dtype: int64
***********************************
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("\t ")
data_types=df.dtypes
print(data_types)
print()
print("\tCOLUMN LABELS")
print("\t ")
print(df.columns.values)
print()
print("\tROW LABELS")
print(df.index.values)
print()
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
Rollno int64
Name object
Test1 int64
Test2 int64
Test3 float64
Test4 int64
dtype: object
COLUMN LABELS
ROW LABELS
[0 1 2 3 4 5]
>>>
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(df)
df.set_index('Rollno',inplace=True)
print()
print(df)
print()
print("\t ")
print(df[0:3:2])
print()
print("\t ")
print(index,values)
print()
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("\t ")
df=df.drop(3,axis=0)
print(df)
OUTPUT
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
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
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
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
2 Name Priya
Age 15
M1 66
M2 97
M3 92
M4 70
M5 70
Total 395
3 Name Lekha
Age 16
M1 40
M2 49
M3 78
M4 84
M5 69
Total 320
4 Name Ram
Age 15
M1 82
M2 72
M3 84
M4 92
M5 52
Total 382
5 Name Shreya
Age 17
M1 93
M2 84
M3 66
M4 40
M5 46
Total 329
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
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(" -")
print(Report[Report.Percentage>80])
print(" -")
print(Report[(Report.Stream=='Commerce') | (Report.Stream=='Science')])
print(" ")
print(" ")
Report.loc[Report.Stream=='Math','Percentage']+=2
print(Report)
print(" ")
print(Report.drop(['Age','Stream'],axis=1))
OUTPUT
0 Ankit 21 Math 88
1 Amit 19 Commerce 92
2 Aishwarya 20 Science 95
1 Amit 19 Commerce 92
2 Aishwarya 20 Science 95
5 Sreya 21 Science 78
0 Ankit 21 Math 88
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
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):
L1.append(name)
L2.append(population)
L3.append(income)
Object1_Population=pd.Series(L2,L1)
Object2_AvgIncome=pd.Series(L3,L1)
Object3_PerCapitaIncome=(Object2_AvgIncome/Object1_Population)
city,Object1_Population,sep="\n")
city",Object2_AvgIncome, sep="\n")
12000000
income 34500
15000000
12500000
3400000
9000000
Enter the average income 4600
Chennai 12000000
Delhi 1300000
Mumbai 15000000
Kolkotta 12500000
Bangalore 3400000
Hyderabad 9000000
dtype: int64
Chennai 12450.0
Delhi 34500.0
Mumbai 10500.0
Kolkotta 4900.0
Bangalore 10000.0
Hyderabad 4600.0
dtype: float64
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
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]
Sales.loc["Karna"]=[196.2,37800,52000,78438,38852]
Sales=Sales.drop(2014,axis=1)
Sales=Sales.rename({"Kanaga":"Kishore","Kamala":"Gridhar","Banu":"Badri"},axis=
0)
Sales.loc["Badri",2018]=100000
Madhu 50000
Kanaga 60000
Kamala 70000
Banu 80000
Dharma 90000
2017 2018
>>>
PROGRAM 21
Create a Data Frame quarterly_sales where each row contains the item
category, itemname, make year and expenditure.
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
Grade={‘Name’:[‘Rashmi’,’Harsh’,’Ganesh’,’Priya’,’Vivek’,’Anita’,’Kartik’],
’Grade’:[‘a1’,’a2’,’b1’,’a1’,b2’,’a2’,’a1’]}
i)Create dataframe Gr
[92,89,None,95,68,None,93]
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:
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
Df=pd.read_csv("C:\\student_result.csv",sep=",")
print(df)
df.to_csv('C:\\copyStudent_result.csv',columns=['Admno',"Name","Perc
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")
Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
plt.xlabel("Year")
plt.ylabel("Unemployment_Rate")
plt.plot(Year,Unemployment_Rate,'g',linewidth=5,linestyle='dashed')
plt.show()
OUTPUT
PROGRAM 25
performance = [10,8,6,3,1]
plt.xlabel("Programming Languages")
plt.ylabel("Performance")
plt.bar(objects,performance,width=0.9,color=['r','b','y','g'])
plt.show()
OUTPUT
PROGRAM 26
import numpy as np
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
dict1={"Name":["Anju","Rishi","Riya","Priya","Shiva"],
"Total":[543,497,398,589,534]}
df1=pd.DataFrame(dict1,index=["Anju","Rishi","Riya","Priya","Shiva"])
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.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.grid()
pl.legend()
pl.show()
OUTPUT
PROGRAM 30
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).
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.show()
output
PROGRAM 32
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.grid()
plt.legend()
plt.show()
OUTPUT
PROGRAM 33
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]
data=[ 9,10,11,13,13,15,16,17,18,19,21,23,23,23,24,24,25,
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')
color='r', fontsize=16)
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
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)
for i in range(n):
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 name=Aliya
Enter Age=15
Enter city=chennai
Enter Salary=25000
Enter name=Syed
Enter Age=16
Enter city=delhi
Enter Salary=35000
Enter name=mateen
Enter Age=15
Enter city=ooty
Enter Salary=25000
Enter name=Meena
Enter Age=17
Enter city=kodai
Enter Salary=27000
Enter name=fahima
Enter Age=16
Enter city=chennai
Enter Salary=29000
>>>
PROGRAM 36
import csv
f=open(r"empxyz.csv","r")
csvr=csv.reader(f)
found=0
for r in csvr:
if (r[0]==eno):
print(r)
found=1
f.close()
if found==0:
else:
f.close()
OUTPUT:
import csv
f=open(r"empxyz.csv","r")
csvr=csv.reader(f)
found=0
li=[]
for r in csvr:
if (r[0]!=eno):
li.append(r)
else:
found=1
f.close()
if found==0:
else:
f=open("empxyz.csv","w", newline='')
csvr=csv.writer(f)
csvr.writerows(li)
f.close()
OUTPUT:
>>>
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.
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
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'))
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 locationCHENNAI
Enter orderid2
Enter ordernameORANGE
Enter price67
Enter locationOOTY
Enter orderid3
Enter ordernameGRAPES
Enter price45
Enter locationKODAI
Enter orderid4
Enter ordernameBADAM
Enter price600
Enter delivery charge34
Enter locationDELHI
Enter orderid5
Enter ordernameFIGS
Enter price1000
Enter locationCHENNAI
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):
b=input('enter subject')
c=input('Enter Booktitle')
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)
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
entersubjectCOMPUTER SCIENCE
Enter BooktitlePYTHON
Enter price340
entersubjectACCOUNTANCY
Enter BooktitleIYER
Enter price700
entersubjectPYTHON
Enter BooktitlePANDAS
Enter price800
entersubjectENGLISH
Enter BooktitleFIRE
Enter price550
entersubjectMATHS
Enter BooktitleTRIANGLE
Enter price900
import pandas as pd
importmatplotlib.pyplot as plt
l1=[]
l2=[]
l3=[]
l4=[]
d={}
for i in range(3):
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 population23456543
Enter Date12/10/2022
Enter population4566432
Enter Date10/09/2021
Enter population453232
Enter Date23/05/2022
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):
i=int(input())
il.append(i)
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.xlabel('Overs')
plt.ylabel('Runs')
plt.legend(['India','Australia'])
plt.show()
OUTPUT
200
250
350
299
399
450
450
399
465
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.
import pandas as pd
importmatplotlib.pyplot as plt
lst1=[]
lst2=[]
name=["KARTHIK","ANAS","AFRIN","SALMAN","AIYESHA"]
fori in name:
a=float(input())
lst1.append(a)
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
98
99
100
99
89
95
98
95
enter the marks of AIYESHA in the first test
100
100
TEST1 TEST2
NAME
NAME
import pandas as pd
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
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
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.legend('Salary', loc='best')
pl.show()
output