Info Practical
Info Practical
Info Practical
PANDAS SERIES-1
1a) To create a series from a list with values one, two, three, four, five, six
To print the series and give it the index values of ‘a’, ‘b’, ‘c’, ‘d’, ‘e’
PROGRAM
import pandas as pd
s1=pd.Series(['one','two','three','four','five','six'])
print(s1)
s1=pd.Series(['one','two','three','four','five','six'],index=['a','b','c','d','e','f'])
print(s1)
1
OUTPUT
2
1B)Write a python program to perform the following operations:-
PROGRAM
import pandas as pd
D1={'a':100,'b':200,'c':300,'d':400,'e':500}
print("original dictionary")
print(D1)
S1=pd.Series(D1)
print(S1)
3
OUTPUT
4
PYTHON PROGRAM-II
PANDAS SERIES-2
2) To perform basic arithmetical operation of addition, subtraction,
multiplication ,division, and integer division on two series s1 and s2 with
values
PROGRAM
import pandas as pd
s1=pd.Series(range(10,60,10),index=[1,2,3,4,5])
s2=pd.Series(range(2,12,2),index=[1,2,3,4,5])
#printing series s1
print("series s1")
print(s1)
#printing series s2
print("series s2")
print(s2)
s3=s1+s2
print(s3)
s3=s1-s2
5
print(s3)
s3=s1*s2
print(s3)
s3=s1/s2
print(s3)
s3=s1//s2
print(s3)
6
OUTPUT
7
8
PYTHON PROGRAM – III
PANDAS SERIES-III
PROGRAM
import pandas as pd
import numpy as np
A=np.array([10,20,30,40,50,60,70,80,90,100])
print(A)
S=pd.Series(A,index=['A','B','C','D','E','F','G','H','I','J'])
print(S)
9
OUTPUT
10
3ii) Using arange function with default index values
PROGRAM
import pandas as pd
import numpy as np
A=(np.arange(10,110,10))
print(A)
S=pd.Series(A)
print(S)
11
OUTPUT
12
PYTHON PROGRAM – IV
PANDAS SERIES-IV
To retrieve MARKS in a Series S1 based on the conditions given below. The list of
marks are [45.5,67.75,89.5,33.25,90.75,70.0,29.25,95.5]
i. Display all marks with value less than or equal to 40.
ii. Display all marks with values greater than or equal to 90.
iii. Display all marks not less than 60
iv. Display all marks not above 40.
v. Display all the marks other than 70.
PROGRAM
import pandas as pd
S1=pd.Series([45.5,67.75,89.5,33.25,90.75,70.0,29.25,95.5])
print("PRINTING ORIGINAL SERIES")
print(S1)
print("MARKS LESS THAN OR EQUAL TO 40") print(S1[S1<=40])
print("MARKS GREATER THAN OR EQUAL TO 90") print(S1[S1>=90])
print(S1[S1>=60])
13
OUTPUT
14
PYTHON PROGRAM – V
DATA FRAME-I
5.A)Write a Pandas program to join the two given data frames along rows and
assign all data. Data Frames are converted from dictionaries Dic1 and Dic2 with
the keys as name, perc and qualify with values
Dic1 = {'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']}
Dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'], 'perc': [89.5, 92, 90.5,
91.5, 90], 'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
PROGRAM
import pandas as pd
import numpy as np
Dic1 = {'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']}
exam_data1 = pd.DataFrame(Dic1)
Dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Sherin', 'Hibah'],
'perc': [89.5, 92, 90.5, 91.5, 90], 'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(Dic2)
print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
15
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)
OUTPUT
16
5.B) Write a Pandas program to join the two given data frames along
columns and assign all data. Data Frames are converted from
dictionaries Dic1 and Dic2 with the keys name, perc and qualify with
values
Dic1 = {'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']}
Dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Sherin', 'Hibah'],
'perc': [89.5, 92, 90.5, 91.5, 90], 'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
PROGRAM
import pandas as pd
import numpy as np
Dic1 = {'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']}
exam_data1 = pd.DataFrame(Dic1)
Dic2 = {'name': ['Parveen', 'Ahil', 'Asim', 'Sherin', 'Hibah'],
'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(Dic2)
print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
17
print(exam_data2)
print("\nJoin the said two dataframes along columns:")
result_data = pd.concat([exam_data1, exam_data2],axis=1)
print(result_data)
OUTPUT
18
PYTHON PROGRAM – VI
DATA FRAME-II
To create a Data Frame DF as shown below and perform the following operations
on it.
1 ANN 60 89 90 80 80
PROGRAM
import pandas as pd
S={'RNO':[1,2,3,4,5],'NAME':['ANN','TOM','MARY','SMITH','BLAKE'],'ENG':[6
0,70,55,80,75],'PHY':[89,67.5,55.75,60.5,56.25],'ECO':[90,78.5,69.25,40,67.5],'IN
FO':[80,86.5,56,73,85.25], 'MATHS':[80,89.5,76.25,60.5,40.5]}
DF=pd.DataFrame(S)
print("PRINTING ORIGINAL DATA FRAME")
19
print(DF)
print("SORTING BY ALPHABETICAL ORDER OF NAMES")
print(DF.sort_values('NAME'))
print("SORTING BY DESCENDING ORDER OF MATHS")
print(DF.sort_values('MATHS',ascending=False))
print("SORTING BY DECREASING ORDER OF RNO AND ASCENDING
ORDER OF NAMES")
print(DF.sort_values(['RNO','NAME'],ascending=[False,True]))
print("SORTING BY DESCENDING ORDER OF MATHS AND ASCENDING
ORDER OF INFO")
print(DF.sort_values(['MATHS','INFO'],ascending=[False,True]))
print("TOTAL MARKS FOR EACH STUDENT")
DF['TOTAL']=DF['ENG']+DF['PHY']+DF['ECO']+DF['INFO']+DF['MATHS']
print(DF)
print("AVERAGE MARKS FOR EACH STUDENT")
DF['AVERAGE']=DF['TOTAL']/5
print(DF)
20
OUTPUT
21
22
PYTHON PROGRAM – VII
DATA FRAME-III
I. To create a Data Frame DF as shown below and perform the following
operations on it.
23
PROGRAM
import pandas as pd
G={'NO':[1,2,3,4,5],'NAME':['JUGAL','SHARMILA','SANDEEP','SANGEETHA'
,'RAKESH'],'AGE':[34,31,32,35,42],'DEPT':['COMPUTER','HISTORY','MATHS',
'HISTORY','COMPUTER'],'DOJ':['1994-01-10','1998-03-24','1996-12-12','1999-
07-01','1998-06-27'],'SALARY':[10000,12000,20000,15000,16000]}
DF=pd.DataFrame(G)
print(DF)
print()
print("FIRST 3 ROWS OF DATA FRAME")
print(DF.head(3))
print()
print("LAST 4 ROWS OF DATA FRAME")
print(DF.tail(4))
print()
print("ADDING A NEW COLUMN GENDER TO THE ABOVE DATA
FRAME")
DF['GENDER']=['M','F','M','F','M']
print(DF)
print()
print("ADDING A NEW ROW TO THE ABOVE DATA FRAME")
N={'NO':6,'NAME':'MEGHNA','AGE':24,'DEPT':'ECONOMICS','DOJ':'2017-01-
01', 'SALARY':15000,'GENDER':'F'}
DF=DF.append(N,ignore_index=True)
print(DF)
print()
print(" DISPLAYING THE COLUMN LABELS")
print(DF.columns)
24
print()
print("DISPLAYING THE ROW LABELS")
print(DF.index)
print()
print("DISPLAYING DATA TYPES OF EACH COLUMN")
print(DF.dtypes)
print()
print("DIMEMNSION OF DATA FRAME")
print(DF.ndim)
print()
print("DISPLAYING TOTAL NUMBER OF ELEMENTS OF DATA FRAME")
print(DF.size)
print("DISPLAYING COLUMN AGE")
print(DF['AGE'])
print("DISPLAYING MAXIMUM AGE FROM THE COLUMN AGE")
max = DF['AGE'].max()
print(" MAXIMUM AGE IS",max)
print()
print("DISPLAYING COLUMN SALARY")
print(DF['SALARY'])
print("DISPLAYING MINIMUM SALARY FROM THE COLUMN SALARY")
min = DF['SALARY'].min()
print("MINIMUM SALARY IS",min)
print( )
print("REMOVING GENDER COLUMN FROM DATA FRAME")
25
print(DF.drop('GENDER',axis=1))
print( )
print("REMOVING 5TH ROW FROM DATA FRAME")
print(DF.drop(4))
OUTPUT
26
27
28
PYTHON PROGRAM – VIII
DATA FRAME-IV
Create the following DataFrame Sales containing year wise sales figures for five
sales persons in INR. Use the years as column labels, and sales person names as
row labels.
29
PROGRAM
import pandas as pd
S={'2017':[100.5,150.8,200.9,330.5,400.75],'2018':[12000,18000,22000,30000,450
00],'2019':[20000,50000,70000,100000,125000],'2020':[50000,60000,70000,80000
, 90000]}
SALES=pd.DataFrame(S,index=['ANN','SHRUTI','RAJ','ANKIT','JUGAL'])
print("ORIGINAL DATA FRAME")
print(SALES)
print()
print("DIMENSION AND SHAPE OF SALES")
print(SALES.ndim)
print(SALES.shape)
print("SIZE AND VALUES OF SALES")
print(SALES.size)
print(SALES.values)
print( ) print("FIRST TWO COLUMNS OF SALES")
print(SALES[['2017','2018']])
print( )
print("LAST THREE ROWS OF SALES")
print(SALES.loc['RAJ':'JUGAL'])
print()
print("SALES MADE BY ALL SALESPERSON IN 2020")
print(SALES['2020'])
print( )
print("SALES MADE BY ANN AND RAJ IN 2017 AND 2018")
print(SALES.loc['ANN':'RAJ':2,'2017':'2018'])
print()
print("SALES MADE BY SHRUTI IN 2018")
30
print(SALES.loc['SHRUTI','2018'])
print()
print("UPDATING SALES MADE BY ANKIT IN 2020 TO 85000")
SALES.loc['ANKIT','2020']=85000
print(SALES)
print()
print("DELETING DATA FOR THE YEAR 2019 FROM SALES")
print()
print(SALES.drop(['2019'],axis=1))
print( )
print("DELETING DATA FOR SALESMAN JUGAL FROM SALES")
print(SALES.drop(['JUGAL']))
print()
print("TRANSPOSE OF DATAFRAME SALES")
print(SALES.T)
31
OUTPUT
32
33
34
PYTHON PROGRAM – IX
PYPLOT-I
a) Create multiple line charts on common plot where three data ranges are
plotted on same chart. The data ranges to be plotted are
Data = [[5., 25., 45., 20.,15. ], [ 8., 13., 29., 27.,18. ], [ 9., 29., 27., 39.,22.] ]
Ensure to have the following specifications on the chart
I. Line should be red in color with width of 3 and dashed dotted
line style
II. Line should be green in color with width of 4 and dotted style
III. Line should be blue in color with width of 2 and dashed line
style
IV. The legends should be LINE-I, LINE-II and LINE-III and it
should be placed in the upper left corner
V. X Axis label should be X VALUES [0,1,2,3,4]
VI. Y Axis label should be Y VALUES [DATA VALUES]
VII. Title of your chart should be MULTI RANGE LINE CHART
PROGRAM
import numpy as np
import matplotlib.pyplot as plt
Data = [[5., 25., 45., 20.,15. ], [ 8., 13., 29., 27.,18. ], [ 9., 29., 27., 39.,22.] ]
X=np.arange(5)222
plt.plot(X,Data[0], color='r',linewidth=3,linestyle='-.',label='LINE-I')
plt.plot(X,Data[1], color='g',linewidth=4,linestyle=':',label='LINE-II')
plt.plot(X,Data[2], color='b',linewidth=2,linestyle='--',label='LINE-III')
plt.legend(loc=2)
plt.title("Multi Range Line Chart")
plt.xlabel('X VALUES [0,1,2,3,4]')
plt.ylabel('Y VALUES [DATA VALUES]')
plt.show()
35
OUTPUT
36
b)Write a program to plot a line chart to depict a comparison of population in
millions between India and Pakistan from 1960 to 2010
37
OUTPUT
38
PYTHON PROGRAM – X
PYPLOT-II
A. Write a Python Program to plot a bar chart from the medals won by top four
countries AUSTRALIA, ENGLAND, INDIA and CANADA in COMMON
WEALTH GAMES.
MEDAL TALLY OF COMMON WEALTH GAMES 2018
39
PROGRAM
import numpy as np
import matplotlib.pyplot as p
INFO=['GOLD','SILVER','BRONZE','TOTAL']
A=[80,59,59,198]
E=[45,45,46,136]
I=[26,20,20,66]
C=[15,40,27,82]
X=np.arange(len(INFO))
p.bar(X,A,WIDTH=.15,color='b',label='AUTRALIA')
p.bar(X+.15,E,WIDTH=.15,color='y',label='ENGLAND')
p.bar(X+.30,I,WIDTH=.15,color='r',label='INDIA')
p.bar(X+.45,C,WIDTH=.15,color='g',label='CANADA')
p.xlabel("MEDAL TYPE")
p.ylabel("TOP FOUR COUNTRIES TALLY")
p.title("COMMON WEALTH GAMES - 2018")
p.legend(loc=2)
40
OUTPUT
41
B. Write a Python Program to plot a horizontal bar chart for INDIA’S MEDAL
TALLY from the table given below
MEDAL TALLY OF COMMON WEALTH GAMES 2018
COUNTRY GOLD SILVER BRONZE TOTAL
AUSTRALIA 80 59 59 198
ENGLAND 45 45 46 136
INDIA 26 20 20 66
CANADA 15 40 27 82
42
PROGRAM
import numpy as np
import matplotlib.pyplot as p
INFO=['GOLD','SILVER','BRONZE','TOTAL']
I=[26,20,20,66]
X=np.arange(len(INFO))
p.barh(X,I,color=['gold','silver','brown','black'])
p.xlabel("MEDAL COUNT")
p.ylabel("MEDAL TYPE")
p.title("INDIA'S MEDAL TALLY IN COMMON WEALTH GAMES - 2018")
p.grid()
p.show()
OUTPUT
43
PYTHON PROGRAM – XI
PYPLOT-III
Write a Python Program to create the following data frame DF
RNO TERM I TERM II TERM III
1 75.5 70.25 68
2 54.5 45.5 50.25
3 89.75 90.5 80.5
4 60.25 69.25 65.75
5 90.25 95 94.25
44
PROGRAM
import pandas as pd
import matplotlib.pyplot as p
D={'RNO':[1,2,3,4,5],'TERM I':[75.5,54.5,89.75,60.25,90.25],'TERM
II':[70.25,45.5,90.5,69.25,95],'TERM III':[68,50.25,80.5,65.75,94.25]}
DF=pd.DataFrame(D)
print(DF)
DF.plot(x="RNO",y=["TERM I","TERM II","TERM III"],
kind="bar",color=['r','b','g'])
p.title("RESULT ANALYSIS")
p.xlabel("ROLL NUMBERS")
p.ylabel("AVERAGE MARKS OF TERM I,II AND III")
DF.plot(x="RNO",y=["TERM I","TERM II","TERM III"],
kind="line",color=['r','b','g'])
p.title("RESULT ANALYSIS")
p.xlabel("ROLL NUMBERS")
p.ylabel("AVERAGE MARKS OF TERM I,II AND III")
p.show()
45
OUTPUT FOR BAR CHART
46
PYTHON PROGRAM – XII
PYPLOT-IV
a) Given below are the sugar levels for men and women in a city. Compare the
sugar levels among them through a histogram.
MEN=[113,85,90,150,149,88,93,115,135,80,77,82,129]
WOMEN=[67,98,89,120,133,150,84,69,89,79,120,112,100]
Ensure the following specifications in the chart
Title should be Blood Sugar Analysis
X axis label should be Sugar Range
Y axis label should be Total Number Of patients
Color for Men should be Red and for women should be black
Legends MEN and WOMEN should be placed in the upper right corner of
your chart
PROGRAM
import matplotlib.pyplot as p
MEN=[113,85,90,150,149,88,93,115,135,80,77,82,129]
WOMEN=[67,98,89,120,133,150,84,69,89,79,120,112,100]
p.hist([MEN,WOMEN],bins=[60,80,100,120,140,160],color=['red','black'],label=['
MEN','WOMEN'])
p.xlabel("SUGAR RANGE")
p.ylabel("TOTAL NUMBER OF PATIENTS")
p.title("BLOOD SUGAR ANALYSIS")
p.legend(loc=1)
p.show( )
47
OUTPUT
48
b) Modify the above code to display the histogram horizontally and to display
the legend in the lower right corner.
PROGRAM
import matplotlib.pyplot as p
MEN=[113,85,90,150,149,88,93,115,135,80,77,82,129]
WOMEN=[67,98,89,120,133,150,84,69,89,79,120,112,100]
p.hist([MEN,WOMEN],bins=[60,80,100,120,140,160],color=['red','black'],label=['
MEN','WOMEN'],orientation='horizontal')
p.xlabel("SUGAR RANGE")
p.ylabel("TOTAL NUMBER OF PATIENTS")
p.title("BLOOD SUGAR ANLYSIS")
p.legend(loc=4)
p.show( )
49
OUTPUT
50
PYTHON PROGRAM – XIII
CSV FILES - I
i) To read from a CSV file EMP.csv and create a data frame from it. Emp.csv
file is having the following data
ii) Display the data frame from the above CSV file with column headings as
0,1,2,3
iii) Display the data frame from the above CSV file and give columns headings
as NO,NAME,DESIGNATION and PAY
iv) Display the first 3 rows of data frame from the above CSV file .
v) Display the middle row of the Data Frame from the above csv file.
51
PROGRAM
import pandas as pd
print("DISPLAYING DATA FRAME FROM A CSV FILE")
DF=pd.read_csv("C:\\Users\\User\\Desktop\\EMP.csv")
print(DF)
print()
print("DISPLAYING DATA FRAME FROM A CSV FILE WITH DEFAULT
COLUMNS INDEX 0,1,2,3")
DF=pd.read_csv("C:\\Users\\User\\Desktop\\EMP.csv",skiprows=1,header=None)
print(DF)
print()
print("DISPLAYING DATA FRAME FROM A CSV FILE WITH COLUMNS
NAMES AS NO,NAME,DESIGNATION AND SAL")
DF=pd.read_csv("C:\\Users\\User\\Desktop\\EMP.csv",names=['NO','NAME','DE
SIGNATION','SAL'],skiprows=1)
print(DF)
print()
print("DISPLAYING DATA FRAME FROM A CSV FILE AND EXTRACT
ONLY THE FIRST 3 ROWS")
DF=pd.read_csv("C:\\Users\\User\\Desktop\\EMP.csv",nrows=3)
print(DF)
print()
print("DISPLAYING DATA FRAME FROM A CSV FILE AND EXTRACT
MIDDLE ROW")
DF=pd.read_csv("C:\\Users\\User\\Desktop\\EMP.csv",nrows=3)
print(DF.tail(1))
52
OUTPUT
53
PYTHON PROGRAM – XIV
CSV FILES – II
i) To create a Data Frame from the given table
DEPTNO DEPTNAME LOC NOE
10 SALES DELHI 50
20 FINANCE NaN 40
30 ACCOUNTS MUMBAI 60
40 HR COCHIN 30
50 NaN DELHI 65
ii) Write a program to convert the contents of this data frame to a csv file
iii) Convert the Data Frame to CSV file with separator *
iv) Convert the Data Frame to CSV file with separator # and replace NaN
with NULL
PROGRAM
import pandas as pd
import numpy as np
D={'DEPTNO':[10,20,30,40,50],'DEPTNAME':['SALES','FINANCE','ACCOU
NTS','HR',np.nan],'LOC':['DELHI',np.nan,'MUMBAI','COCHIN','DELHI'],'NO
E':[50,40,60,30,65]}
DF=pd.DataFrame(D)
print("ORIGINAL DATA FRAME")
print(DF)
print()
print("WRITING TO A CSV FILE")
DF.to_csv("C:\\Users\\User\\Desktop\\DEPT.csv")
print("CONVERTING TO A CSV FILE WITH * AS SEPARATOR")
DF.to_csv("C:\\Users\\User\\Desktop\\DEPT1.csv",sep='*')
print("CONVERTING TO A CSV FILE WITH # AS SEPARATOR AND
REPLACE NaN WITH NULL")
DF.to_csv("C:\\Users\\User\\Desktop\\DEPT2.csv",sep='#',na_rep='NULL')
54
OUTPUT
55
CONVERTING TO A CSV FILE WITH $ AS SEPERATOR AND REPLACE
NaN WITH NULL
56