Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Info Practical

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

PYTHON PROGRAM-I

PANDAS SERIES-1
1a) To create a series from a list with values one, two, three, four, five, six

To print the series with default index values

To print the series and give it the index values of ‘a’, ‘b’, ‘c’, ‘d’, ‘e’

PROGRAM
import pandas as pd

print("series with default index values")

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("series with explicitly defined index values")

print(s1)

1
OUTPUT

2
1B)Write a python program to perform the following operations:-

a) Create a pandas series from a dictionary of values given below

original dictionary={‘a’:100, ‘b’:200, ‘c’:300, ‘d’:400, ‘e’:500}

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("dictionary converted into series")

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

s1=[10,20,30,40,50] & s2=[2,4,6,8,10]

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

#printing sum of s1 & s2

print("sum of s1 & s2")

print(s3)

s3=s1-s2

#printing difference of s1 & s2

print("difference of s1 & s2")

5
print(s3)

s3=s1*s2

#printing product of s1 & s2

print("product of s1 & s2")

print(s3)

s3=s1/s2

#printing the quotient of s1 & s2

print("quotient of s1 & s2")

print(s3)

s3=s1//s2

#printing integer quotient of s1 & s2

print("integer quotient of s1 & s2")

print(s3)

6
OUTPUT

7
8
PYTHON PROGRAM – III
PANDAS SERIES-III

3.i) To create a Series from an array A of values [10 20 30 40 50 60 70 80 90 100]


Without range function with index values A,B,C,D,E,F,G,H,I,J

PROGRAM
import pandas as pd

import numpy as np

A=np.array([10,20,30,40,50,60,70,80,90,100])

print("PRINTING ARRAY ELEMENTS")

print(A)

S=pd.Series(A,index=['A','B','C','D','E','F','G','H','I','J'])

print("PRINTING SERIES ELEMENT FROM ARRAY WITH

DEFINED INDEX VALUES")

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("PRINTING ARRAY ELEMENTS")

print(A)

S=pd.Series(A)

print("PRINTING SERIES ELEMENT FROM ARRAY")

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("MARKS NOT LESS THAN 60")

print(S1[S1>=60])

print("MARKS NOT ABOVE 40")


print(S1[S1<=40])
print("MARKS NOT EQUAL TO 70")
print(S1[S1!=70])

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.

RNO NAME ENG PHY ECO INFO MATHS

1 ANN 60 89 90 80 80

2 TOM 70 67.5 78.5 86.5 89.5

3 MARY 55 55.75 69.25 56 76.25

4 SMITH 80 60.5 40 73 60.5

5 BLAKE 75 56.25 67.5 85.25 40.5

i) Sort the Table in alphabetical order of names.


ii) Sort the table in descending order of MATHS marks
iii) Sort the table First by decreasing order of RNO and ascending order of
Names
iv) Descending order of MATHS and Ascending order of INFO marks
v) To get the Total Marks for each student
vi) To get the Average Marks for each student

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.

NO NAME AGE DEPT DOJ SALARY


1 JUGAL 34 COMPUTER 1994-01-10 10000
2 SHARMILA 31 HISTORY 1998-03-24 12000
3 SANDEEP 32 MATHS 1996-12-12 20000
4 SANGEETHA 35 HISTORY 1999-07-01 15000
5 RAKESH 42 COMPUTER 1998-06-27 16000

II. To display the first 3 rows of the Data Frame


III. To display the last 4 rows of the Data frame
IV. To add a new column Gender to the above Data frame
V. To add a new row to the above Data Frame
VI. To display the row labels and column labels of the above Data frame
VII. To display the data type of each column and dimension of the above Data
Frame
VIII. To display the total number of elements of the above Data Frame
IX. To display the column age and its maximum value from the above Data
Frame.
X. To dusplay the column Salary and its minimum value from the above Data
Frame
XI. To remove the column Gender added in the above Data Frame
XII. To remove the 5th row of the Data Frame

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.

2017 2018 2019 2020


ANN 100.5 12000 20000 50000
SHRUTI 150.8 18000 50000 60000
RAJ 200.9 22000 70000 70000
ANKIT 300 30000 100000 80000
JUGAL 400 45000 125000 90000

I. Display the dimensions, shape, size and values of Sales.


II. Display the first 2 columns of Sales.
III. Display the last 3 rows of Sales without using tail function.
IV. Display the sales made by all salespersons in the year 2020.
V. Display the sales made by ANN and RAJ in the year 2017 and 2018.
VI. Display the sales made by SHRUTI in 2018
VII. Update the sale made by ANKIT in 2018 to 85000
VIII. Delete the data for the year 2019 from the DataFrame Sales.
IX. Delete the data for sales man JUGAL from the DataFrame Sales.
X. Change the DataFrame Sales such that it becomes its transpose

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

YEAR 1960 1970 1980 1990 2000 2010

INDIA 449.48 553.57 696.783 870.133 1000.4 1309.1

PAKISTAN 44.91 58.09 78.07 107.7 138.5 170.6

The line chart should have the following specifications


i) Line chart for India should be red and for Pakistan it should be green.
ii) Line Thickness for India should be 4 and for Pakistan should be 2
iii) Line style for India should be Solid and for pakistan should be dashed dot
iv) Legends INDIA and PAKISTAN should be placed in upper left corner.
v) X Axis Label should be YEAR
vi) Y Axis Label should be POPULTION IN MILLIONS
vii) Title of the chart should be POPULATION COMPARISON B/W INDIA
AND PAKISTAN
PROGRAM

import matplotlib.pyplot as plt


I=[449.48,553.57,696.783,870.133,1000.4,1309.1]
P=[44.91,58.09,78.07,107.7,138.5,170.6]
YR= [1960,1970,1980,1990,2000,2010]
plt.plot(YR,I,color='r',linewidth=4,linestyle='-',label='INDIA')
plt.plot(YR,P, color='g',linewidth=2,linestyle='-.',label='PAKISTAN')
plt.legend(loc=2)
plt.title("POPULATION COMPARISON B/W INDIA AND PAKISTAN")
plt.xlabel('YEAR')
plt.ylabel('POPULATION IN MILLIONS')
plt.show()

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

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

Ensure that the chart has the following specifications


i) The color for AUSTRALIA,ENGLAND,INDIA and CANADA should
be BLUE,YELLOW,RED and GREEN
ii) The width of the bars should be .15
iii) X AXIS label should be MEDAL TYPE
iv) Y AXIS label should be TOP FOUR COUNTRIES TALLY
v) Title of the chart should be COMMON WEALTH GAMES – 2018
vi) Legends AUSTRALIA, ENGLAND, INDIA and CANADA should be in
the upper left corner.

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

Ensure the following specifications


i) Color for the medals should be GOLD,SILVER,BROWN and BLACK
ii) X AXIS label should be MEDAL COUNT
iii) Y AXIS label should be MEDAL TYPE
iv) Title of the chart should be COMMON WEALTH GAMES INDIA – 2018
v) Display the chart in a grid.

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

I. After creating the Data Frame do the following operation on it.


II. Plot a line chart for the roll number of each student against Term1, Term II
and Term III Average
III. Plot a bar chart for the roll number of each student against Term1, T erm II
and Term III
IV. Term I, Term II and Term III colors should be red, blue and green
V. X AXIS label should be ROLL NUMBERS
VI. Y AXIS label should be AVERAGE MARKS OF TERM I, II AND III
VII. Title of the chart should be RESULT ANALYSIS

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

OUTPUT FOR LINE 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

WRITING TO A CSV FILE

CONVERTING TO A CSV FILE WITH * AS SEPERATOR

55
CONVERTING TO A CSV FILE WITH $ AS SEPERATOR AND REPLACE
NaN WITH NULL

56

You might also like