0% found this document useful (0 votes)
868 views46 pages

Ip Record

The document discusses Python programs to create and manipulate pandas Series objects. It includes programs to create Series from NumPy arrays and dictionaries, add and modify Series elements, perform mathematical operations on multiple Series, and create line and bar charts from Series data to visualize trends. The programs demonstrate fundamental Series operations like indexing, slicing, and changing the index.

Uploaded by

re Hamlinz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
868 views46 pages

Ip Record

The document discusses Python programs to create and manipulate pandas Series objects. It includes programs to create Series from NumPy arrays and dictionaries, add and modify Series elements, perform mathematical operations on multiple Series, and create line and bar charts from Series data to visualize trends. The programs demonstrate fundamental Series operations like indexing, slicing, and changing the index.

Uploaded by

re Hamlinz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

INFORMATICS PRACTICES(065) 2021-22

SERIES FROM NDARRAY

DATE : 3-06-2021
PROGRAM NO : 1
OBJECTIVE :
Write a Python program to create a series from an ndarray and a dictionary of values.

SOURCE CODE :
import pandas as pd
import numpy as np
s1=pd.Series(np.array([23,34,45,67,78]))
print("Series from ndarray")
print(s1)
d1={"Niya":25,"Alfa":24,"Amal":23,"Arun":22}
s2=pd.Series(d1)
print("Series using dictionary")
print(s2)

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 1


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
Series from ndarray
0 23
1 34
2 45
3 67
4 78
dtype: int32
Series using dictionary
Niya 25
Alfa 24
Amal 23
Arun 22
dtype: int64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 2


INFORMATICS PRACTICES(065) 2021-22

DISPLAY PERCENTAGE OBTAINED

DATE : 3-06-2021
PROGRAM NO : 2
OBJECTIVE :
Create a series to display percentage obtained by each student.(The marks is out of 25)
index should be roll no.

SOURCE CODE
import pandas as pd
import numpy as np
marks=np.array([18,22,20,24,25])
data=marks*100/25
s1=pd.Series(data,index=['RNO1','RNO2','RNO3','RNO4','RNO5'])
print("Roll NO .","Percentage")
print(s1)

OUTPUT :
Roll NO . Percentage
RNO1 72.0
RNO2 88.0
RNO3 80.0
RNO4 96.0
RNO5 100.0
dtype: float64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 3


INFORMATICS PRACTICES(065) 2021-22

CREATE A SERIES

DATE : 7-06-2021
PROGRAM NO : 3
OBJECTIVE
Create a series to store the amount of sales made by a salesperson for the last
Year (whole months).
a) Display the sales amount which is greater than 10000.
b) Display the sales amount in the first four months.
c) Display the sales amount in the last four months

SOURCE CODE :
import pandas as pd
amount=[20000,10000,15000,22000,8000,5000,2000,22000,1000,3000,20000, 10000]
months=["Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"]
s1=pd.Series(amount,index=months)
print("Sales amount >10000")
print(s1[s1>10000],"\n")
print("Sales amount for the first four months")
print(s1.head(4),"\n")
print("Sales amount for the last four months")
print(s1.tail(4),"\n")

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 4


INFORMATICS PRACTICES(065) 2021-22

OUTPUT:
Sales amount >10000
Jan 20000
Mar 15000
Apr 22000
Aug 22000
Nov 20000
dtype: int64

Sales amount for the first four months


Jan 20000
Feb 10000
Mar 15000
Apr 22000
dtype: int64

Sales amount for the last four months


Sep 1000
Oct 3000
Nov 20000
Dec 10000
dtype: int64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 5


INFORMATICS PRACTICES(065) 2021-22

SERIES TO STORE THE MARKS

DATE : 7-06-2021
PROGRAM NO : 4
OBJECTIVE:
• Create a series to store the marks in IP of 5 students in a class.
• The ROLLNO should be the index.
• Read the rno of the student and make changes in his/her marks.
• Display the marks of all students after making changes.
• A teacher thought, it will be easy to track students if the names is used as index instead of
roll no.
• Write a program for the above case and display the marks of students with the new index.

SOURCE CODE :
import pandas as pd
marks=[30,20,40,45,35]
rno=["RNO1","RNO2","RNO3","RNO4","RNO5"]
s3=pd.Series(marks,index=rno)
print("ORIGINAL MARKS\n",s3)
chrol=input("Enter the Roll Number:")
chmark=float(input("Enter the New Mark:"))
s3[chrol]=chmark
print("MODIFIED MARKS\n",s3)
names=["Mariya","Kalyani","Neethu","Joy","Joseph"]
s3.index=names
print("AFTER CHANGING THE TINDEX POSITIONS\n",s3)

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 6


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
ORIGINAL MARKS
RNO1 30
RNO2 20
RNO3 40
RNO4 45
RNO5 35
dtype: int64
Enter the Roll Number:RNO2
Enter the New Mark:45
MODIFIED MARKS
RNO1 30
RNO2 45
RNO3 40
RNO4 45
RNO5 35
dtype: int64
AFTER CHANGING THE TINDEX POSITIONS
Mariya 30
Kalyani 45
Neethu 40
Joy 45
Joseph 35
dtype: int64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 7


INFORMATICS PRACTICES(065) 2021-22

CHARITY CONTRIBUTION

DATE : 7-06-2021
PROGRAM NO : 5
OBJECTIVE :
Consider a Series object that stores the charity contribution (in Rs) of each section of a
School as shown below:

XI A 6000
XI B 7800
XI C 6540
XI D 7890
XI E 6890
XI F 7000

Write code to:


a. Modify the amount of section ‘XI A’ as 7600 and for sections ‘XI C’ and ‘XI D’
as 7000 and print the series.
b. Display the first 3 elements (use slicing).
c. Display the last 3 elements (use slicing).
d. Display Third and Fourth elements (using iloc).
e. Display Second, Third and Fourth elements (using loc).

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 8


INFORMATICS PRACTICES(065) 2021-22

SOURCE CODE :
import pandas as pd
s=pd.Series([6700,5600,5000,5200,6200,5800],index=["XI A","XI B","XI C","X1 D","XI
E","XI F"])
print("\nSeries before modifiying the elements:")
print(s)
s[0]=7600
s[2:4]=7000
print("Series after modifiying amounts")
print(s)
print("\n First three elements")
print(s[:3])
print("\n Last three elements")
print(s[-3:])
print("\n Third and Fourth elements(using iloc method):")
print(s.iloc[2:4])
print("\n Second,Third and Fourth elements(using loc method):")
print(s.loc["XI C":"XI E"])

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 9


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
Series before modifiying the elements:
XI A 6700
XI B 5600
XI C 5000
X1 D 5200
XI E 6200
XI F 5800
dtype: int64
Series after modifiying amounts
XI A 7600
XI B 5600
XI C 7000
X1 D 7000
XI E 6200
XI F 5800
dtype: int64

First three elements


XI A 7600
XI B 5600
XI C 7000
dtype: int64

Last three elements


X1 D 7000

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 10


INFORMATICS PRACTICES(065) 2021-22

XI E 6200
XI F 5800
dtype: int64

Third and Fourth elements(using iloc method):


XI C 7000
X1 D 7000
dtype: int64

Second,Third and Fourth elements(using loc method):


XI C 7000
X1 D 7000
XI E 6200
dtype: int64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 11


INFORMATICS PRACTICES(065) 2021-22

MATHEMATICAL OPERATIONS

DATE : 18-06-2021
PROGRAM NO : 6
OBJECTIVE :
Consider 3 Series:
s1=[11,12,13,14], index=[1,2,3,4]
s2=[21,22,23,24], index=[1,2,3,4]
s3=[21,22,23,24], index=[101,102,103,104]
Perform all mathematical operations on them.

SOURCE CODE :
import pandas as pd
s1=pd.Series(data=[11,12,13,14],index=[1,2,3,4])
s2=pd.Series(data=[21,22,23,24],index=[1,2,3,4])
s3=pd.Series(data=[21,22,23,24],index=[101,102,103,104])
print("s1+s2 \m",s1+s2)
print("s1*s2 \n",s1*s2)
print("s1/s2 \n",s1/s2)
print("s1-s2 \n",s1-s2)
print("s1+s3 \n",s1+s3)

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 12


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
s1+s2 \m 1 32
2 34
3 36
4 38
dtype: int64
s1*s2
1 231
2 264
3 299
4 336
dtype: int64
s1/s2
1 0.523810
2 0.545455
3 0.565217
4 0.583333
dtype: float64
s1-s2
1 -10
2 -10
3 -10
4 -10
dtype: int64
s1+s3
1 NaN

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 13


INFORMATICS PRACTICES(065) 2021-22

2 NaN
3 NaN
4 NaN
101 NaN
102 NaN
103 NaN
104 NaN
dtype: float64

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 14


INFORMATICS PRACTICES(065) 2021-22

LINE CHART

DATE : 18-06-2021
PROGRAM NO : 7
OBJECTIVE :
Write a program to draw line chart for the following:
Show the population of India v/s Pakistan from 1960-2010
Ind= [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]
Pak= [44.91, 58.06, 78.07, 107.7, 138.5, 170.6]

SOURCE CODE :
import matplotlib.pyplot as plt
year=[1960,1970,1980,1990,2000,2010]
pop_Ind=[449.48,553.57,696.783,870.133,1000.4,1309.1]
pop_Pak=[44.1,58.06,78.07,107.7,138.5,170.6]
plt.plot(year,pop_Ind,color="r",label="Line-1 INDIA")
plt.plot(year,pop_Pak,color="b",label="Line-2 PAKISTAN")
plt.xlabel("COUNTRIES")
plt.ylabel("POPULATION")
plt.title("India v/s Pakistan Population till 2019")
plt.legend()
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 15


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 16


INFORMATICS PRACTICES(065) 2021-22

DRAW LINE CHARTS

DATE : 18-06-2021
PROGRAM NO : 8
OBJECTIVE :
Write a program to draw line charts for the following:
Show the unemployment rate from 1920 to 2010.
year=[1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
unemployment_rate=[9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]

SOURCE CODE :
import matplotlib.pyplot as plt
year=[1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
unemployment_rate=[9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
plt.plot(year,unemployment_rate)
plt.title("Un-Employment Rate v/s Year")
plt.xlabel("year")
plt.ylabel("Umemployment Rate")
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 17


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 18


INFORMATICS PRACTICES(065) 2021-22

BAR CHART

DATE : 18-06-2021
PROGRAM NO : 9
OBJECTIVE :
Create horizontal bar chart to show the following:
Represent the performance of programming languages:
“python”, ”c++”, ”Java”, ”Scala”, ”lisp” given by different users as
10,8,6,4,2,1 respectively using horizontal bar chart.

SOURCE CODE :
import matplotlib.pyplot as plt
import numpy as np
languages=['python','c++','java','perl','scala','lisp']
y_position=np.arange(len(languages))
performance=[10,8,6,4,2,1]
plt.barh(y_position,performance,align='center',color='r')
plt.yticks(y_position,languages)
plt.xlabel('usage')
plt.title('programming language usage')
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 19


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 20


INFORMATICS PRACTICES(065) 2021-22

CREATE BAR CHART

DATE : 02-07-2021
PROGRAM NO : 10
OBJECTIVE :
Create bar chart to show the following:
Represent class as [“V”,”VI”,”VII”,”VIII”,”IX”,”X”] and strength=[40,38,50,37,43,39]
using a normal bar chart.

SOURCE CODE :
import matplotlib.pyplot as plt
Class=['V','VI','VII','VIII','IX','X']
str=[49,38,50,37,43,39]
c=['r','b','m','k','y','g']
plt.bar(Class,str,color=c)
plt.xlabel("CLASS")
plt.ylabel("STRENGTH")
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 21


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 22


INFORMATICS PRACTICES(065) 2021-22

CREATE A CSV

DATE : 02-07-2021
PROGRAM NO : 11
OBJECTIVE :
Create a csv using the data about cars (name, price in lacks and mileage) as given
below:
‘car’:[‘BMW’,’HONDA’,’TOYOTA’,’HYUNDAI’],’Price’:[100,8,45,7],’Mileage’:[30,27,2
6,26]
Read the above csv and plot a bar chart represents car and its price

SOURCE CODE :
import pandas as pd
import matplotlib.pyplot as plt
dict={'car':['BMW','Honda','Toyota','Hyundai'],'Price':[100,8,45,7],'Mileage':[39,27,26,26]}
df=pd.DataFrame(dict)
df.to_csv("cars.csv")
df2=pd.read_csv("cars.csv")
name=df2['car']
price=df2['Price']
plt.bar(name,price,width=0.5,color='g')
plt.xlabel('CAR')
plt.ylabel('PRICE')
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 23


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 24


INFORMATICS PRACTICES(065) 2021-22

CREATE AN ARRAY HAVING 30 ELEMENTS

DATE : 02-07-2021
PROGRAM NO : 12
OBJECTIVE :
Create an array having 30 elements between 5 and 50. Plot the following
having 10 bins.
i) Simple histogram
ii) Horizontal histogram
iii) Step-Type histogram
iv) Cumulative histogram

SOURCE CODE :
import numpy as np
import matplotlib.pyplot as plt
x=np.random.randint(5,50,30)
plt.hist(x,bins=10,color='g')
plt.title('SIMPLE HISTOGRAM')
plt.show()
x=np.random.randint(5,50,30)
plt.hist(x,bins=10,orientation='horizontal',color='b')
plt.title('HORIZONTAL HISTOGRAM')
plt.show()
plt.hist(x,bins=10,histtype='step',color='b')
plt.title('step-type histogram')
plt.show()
x=np.random.randint(5,50,30)
plt.hist(x,bins=10,cumulative=True,color='m')
plt.title('CUMULATIVE HISTOGRAM')
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 25


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 26


INFORMATICS PRACTICES(065) 2021-22

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 27


INFORMATICS PRACTICES(065) 2021-22

CREATE A DATAFRAME USING A DICTIONARY

DATE ; 02-07-2021
PROGRAM NO : 13
OBJECTIVE :
Create a data frame using a dictionary with the given data:
‘2015’:[256,452,635,965], ‘2016’:[745,478,547],
‘2017’:[452,474,725,854], ‘2018’:[1021,958,528,425]
Index=[‘Qtr1’,’Qtr2’,’Qtr3’,’Qtr4’]
Find the following:
i) Display the data frame
ii) Sales of 2017
iii) Sales in Quarter2
iv) Sales in 2015, 2016 and2017 for Quarter 1 & 2
v) Indices of data frame and columns of data frame
vi) Row and column labels
vii) Number of rows and columns
viii) Sales in Qtr 4 during the year 2018
ix) Change sales in Qtr3 during 2017 to 754 and print
x) Add a new column 2019 with values [524,639,785,458] for all Quarters

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 28


INFORMATICS PRACTICES(065) 2021-22

SOURCE CODE :
import pandas as pd
sale=pd.DataFrame({'2015':[256,452,635,965],'2016':[745,785,478,541],'2017':[452,474,725,
854],'2018':[1021,958,528,425]},index=['qtr1','qtr2','qtr3','qtr4'])
print('data frame')
print(sale)
print('sales of 2017')
print(sale['2017'])
print('sales in quaters2')
print(sale.loc['qtr2',:])
print('sales in 2015,2016 and 2107 for quarter 1 and quarter 2')
print(sale.loc['qtr1':'qtr2','2015':'2017'])
print('indices of dataframe')
print(sale.index)
print('columns of dataframe')
print(sale.columns)
print ('row and columns labels')
print(sale.axes)
print('number of rows and columns')
print(sale.shape)
print('sales in qtr4 during the year 2018')
print(sale.loc['qtr4','2018'])
print('change sale in qtr3 during 2017 to754 and print')
sale.loc['qtr3','2017']=754
print (sale)
print('add a new column 2019 with values [524,639,785,458] for all quaters')
sale[2019]=[524,639,785,458]
print(sale)

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 29


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
data frame
2015 2016 2017 2018
qtr1 256 745 452 1021
qtr2 452 785 474 958
qtr3 635 478 725 528
qtr4 965 541 854 425
sales of 2017
qtr1 452
qtr2 474
qtr3 725
qtr4 854
Name: 2017, dtype: int64
sales in quaters2
2015 452
2016 785
2017 474
2018 958
Name: qtr2, dtype: int64
sales in 2015,2016 and 2107 for quarter 1 and quarter 2
2015 2016 2017
qtr1 256 745 452
qtr2 452 785 474
indices of dataframe
Index(['qtr1', 'qtr2', 'qtr3', 'qtr4'], dtype='object')
columns of dataframe

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 30


INFORMATICS PRACTICES(065) 2021-22

Index(['2015', '2016', '2017', '2018'], dtype='object')


row and columns labels
[Index(['qtr1', 'qtr2', 'qtr3', 'qtr4'], dtype='object'), Index(['2015', '2016', '2017', '2018'],
dtype='object')]
number of rows and columns
(4, 4)
sales in qtr4 during the year 2018
425
change sale in qtr3 during 2017 to754 and print
2015 2016 2017 2018
qtr1 256 745 452 1021
qtr2 452 785 474 958
qtr3 635 478 754 528
qtr4 965 541 854 425
add a new column 2019 with values [524,639,785,458] for all quaters
2015 2016 2017 2018 2019
qtr1 256 745 452 1021 524
qtr2 452 785 474 958 639
qtr3 635 478 754 528 785
qtr4 965 541 854 425 458
>>>

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 31


INFORMATICS PRACTICES(065) 2021-22

CREATE A DATA FRAME TO ENTER

DATE : 31-07-2021
PROGRAM NO : 14
OBJECTIVE :
Create a data frame to enter names of 8 students and their marks.
i. Find the top 3 marks.
ii. Find the lowest 3 marks.

SOURCE CODE :
import pandas as pd
dict={'NAME':['Haridev','Anand','Nihal','Mekhanath','Ashish','Aswin','Lakshmi','Manu']
,'MARKS':[56,78,89,90,79,92,65,85]}
d=pd.DataFrame(dict)
print(d)
print("Top 3 students are:\n")
print(d.nlargest(3,'MARKS'))
print("Bottom 3 students are:\n")
print(d.nsmallest(3,'MARKS'))

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 32


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
NAME MARKS
0 Haridev 56
1 Anand 78
2 Nihal 89
3 Mekhanath 90
4 Ashish 79
5 Aswin 92
6 Lakshmi 65
7 Manu 85
Top 3 students are:

NAME MARKS
5 Aswin 92
3 Mekhanath 90
2 Nihal 89
Bottom 3 students are:

NAME MARKS
0 Haridev 56
6 Lakshmi 65
1 Anand 78

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 33


INFORMATICS PRACTICES(065) 2021-22

READ CSV USING DATA


FRAME

DATE : 31-07-2021
PROGRAM NO : 15
OBJECTIVE :
Create a csv file with id,fname,marks in English,accountancy and ip. Read csv using
Data frame and do the following.
i. Create a bar chart plotting total and performance of English.
ii. Create a histogram plotting number of students Vs their marks in
English,accountancy and ip.
iii. Create a line chart indicate the performance of ip.

SOURCE CODE :
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'id':[1,2,3,4,5,6],'fname':['irfan','ram','alex','hari','joe','jio'],'english':[20,25,
30,35,40,45],'accountancy':[30,35,40,45,50,55],'ip':[50,55,60,65,70,75]})
df['total']=df['english']+df['accountancy']+df['ip']
x=df['total']
y=df['english']
d=plt.bar(x ,y,color='y',width=2.5)
plt.xlabel('total score')
plt.ylabel('english score')
plt.show()

plt.hist([df['english'],df['ip'],df['accountancy']],color=['m','r','b'])
plt.title('number f student ns score')
plt.xlabel('score')

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 34


INFORMATICS PRACTICES(065) 2021-22

plt.ylabel('number of students')
plt.legend(['english','ip','accountancy'])
plt.show()

plt.plot(df['ip'],marker='s',color='g')
plt.title('performance of IP')
plt.xlabel('X axis')
plt.ylabel('y axis')
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 35


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 36


INFORMATICS PRACTICES(065) 2021-22

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 37


INFORMATICS PRACTICES(065) 2021-22

CSV USING DATAFRAME AND DO THE FOLLOWING

DATE : 31-07-2021
PROGRAM NO : 16
OBJECTIVE :
Create a csv file with id,fname marks in English,accountancy and ip. Read
Csv using dataframe and do the following.
i. Display the details of topper.
ii. Display English toppers details.
iii. Create a bar chart compare the performance of each subject.

SOURCE CODE :
import pandas as pd
import matplotlib.pyplot as plt
name=['aparna','pankaj','sudhir','geethu']
eng=[90,40,80,98]
acc=[56,78,45,89]
bs=[78,77,56,66]
ip=[89,87,67,98]
dict={'Name':name,'Eng':eng,'Acc':acc,'BS':bs,'IP':ip}
df=pd.DataFrame(dict)
df.to_csv('filel.csv')
print(df)
df['Total']=df['Eng']+df['Acc']+df['BS']+df['IP']
print('Data Frame after adding Total column is :')
print(df)
print('\n Details Of TOPPER')

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 38


INFORMATICS PRACTICES(065) 2021-22

print(df[df.Total==df.Total.max()])
print('Compare maximum mark of all subjects using bar chart')
max_mark=[df['Eng'].max(),df['Acc'].max(),df['BS'].max(),df['IP'].max()]
sub=['English','Accountancy','Business','IP']
plt.bar(sub,max_mark,width=0.4,color='r',edgecolor='k')
plt.xlabel('Subject')
plt.ylabel('Maximum Marks')
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 39


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
Name Eng Acc BS IP
0 aparna 90 56 78 89
1 pankaj 40 78 77 87
2 sudhir 80 45 56 67
3 geethu 98 89 66 98
Data Frame after adding Total column is :
Name Eng Acc BS IP Total
0 aparna 90 56 78 89 313
1 pankaj 40 78 77 87 282
2 sudhir 80 45 56 67 248
3 geethu 98 89 66 98 351

Details Of TOPPER
Name Eng Acc BS IP Total
3 geethu 98 89 66 98 351
Compare maximum mark of all subjects using bar chart

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 40


INFORMATICS PRACTICES(065) 2021-22

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 41


INFORMATICS PRACTICES(065) 2021-22

CREATE A DATA FRAME USING DICTIONARY

DATE : 31-07-2021
PROGRAM NO : 17
OBJECTIVE :
Create a data frame using dictionary, to show the unemployment rate from
1920 to 2010 using line chart.
Year=[1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate=[9.8,12,7.2,6.9,7,6.5,6.2,5.5,6.3]
Specifications: Chart color-red, marker-circle, font size=14

SOURCE CODE :
import pandas as pd
import matplotlib.pyplot as plt
data={'Year':[1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],'Unemployment_Rate'
:[9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]}
df=pd.DataFrame(data,columns=['Year','Unemployment_Rate'])
plt.plot(df['Year'],df['Unemployment_Rate'],color='red',marker='o')
plt.title('Unemployment_Rate Vs Year',fontsize=14)
plt.xlabel('Year',fontsize=14)
plt.ylabel('Unemployment_Rate',fontsize=14)
plt.grid(True)
plt.show()

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 42


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 43


INFORMATICS PRACTICES(065) 2021-22

CREATE A DATAFRAME USING A DICTIONARY WITH GIVEN DATA

DATE : 31-07-2021
PROGRAM NO : 18
OBJECTIVE :
Create a dataframe using a dictionary with given data
‘Name’:pd.Series([‘Tom’,’James’,’Ricky’,’Vin]),’Age’:pd.Series([25,26,25,23]),
’Rating’:pd.Series([4.23,3.24,3.98,2.56]) and perform the following:
i. Print the number of values in each column .
ii. Print number of values in each row.
iii. Iterate over rows and columns

SOURCE CODE :
import pandas as pd
d={'Name':pd.Series
(['Tom','James','Rickey','Vin']),'Age':pd.Series([25,26,25,23]),'Rating':pd.Series([4.23,3.24,3.
98,2.56])}
df=pd.DataFrame(d)
print('Data Frame')
print(df)
print('Number of values in each column')
print(df.count(axis=1))
print('Iterate over columns:')
for (index,r) in df.iteritems():
print(r)
print('Iterate over rows')
for (index,r) in df.iterrows():
print(r)

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 44


INFORMATICS PRACTICES(065) 2021-22

OUTPUT :
Data Frame
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Rickey 25 3.98
3 Vin 23 2.56
Number of values in each column
0 3
1 3
2 3
3 3
dtype: int64
Iterate over columns:
0 Tom
1 James
2 Rickey
3 Vin
Name: Name, dtype: object
0 25
1 26
2 25
3 23
Name: Age, dtype: int64
0 4.23
1 3.24

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 45


INFORMATICS PRACTICES(065) 2021-22

2 3.98
3 2.56
Name: Rating, dtype: float64
Iterate over rows
Name Tom
Age 25
Rating 4.23
Name: 0, dtype: object
Name James
Age 26
Rating 3.24
Name: 1, dtype: object
Name Rickey
Age 25
Rating 3.98
Name: 2, dtype: object
Name Vin
Age 23
Rating 2.56
Name: 3, dtype: object
>>>

SIVAGIRI VIDYANIKETAN SENIOR SECONDARY SCHOOL, ALUVA 46

You might also like