#PROGRAM 1
# CREATING A PANDAS SERIES FROM A DICTIONARY AND AN
NDARRAY
import pandas as pd
import numpy as np
dictionary={'A':10,'B':20,'C':30}
series=pd.Series(dictionary)
print(series)
print()
s=pd.Series(np.array([1,3,4,7,8,8,9]))
print(s)
#PROGRAM 1
#OUTPUT
runfile('C:/ip practical programs/p1.py', wdir='C:/ip practical programs')
A 10
B 20
C 30
dtype: int64
0 1
1 3
2 4
3 7
4 8
5 8
6 9
dtype: int32
#PROGRAM 2
# FIND THE SUM OF THOSE VALUES WHICH ARE ENDING WITH 3
OR 5
import pandas as pd
import numpy as np
list=[33,55,65,29,19,23]
s=pd.Series(list)
sum5=sum(s[s%10==5])
sum3=sum(s[s%10==3])
print(sum3+sum5)
#PROGRAM 2
#OUTPUT
runfile('C:/ip practical programs/p2.py', wdir='C:/ip practical programs')
176
#PROGRAM 3
# CREATE A SERIES OF 10 NUMBERS STARTING WITH 41 AND
WITH THE INCREMENT OF 3. NOW ADD 7 ALL ODD VALUES AND
SUBTRACT 3 IN EVEN VALUES.REPRINT THE UPDATED SERIES.
import pandas as pd
s=pd.Series(range(41,71,3))
print("Original list:")
print(s)
for i in range(0,s.size):
if s[i]%2==0:
s[i]=s[i]-3
elif s[i]%2!=0:
s[i]=s[i]+7
print("updated list")
print(s)
#PROGRAM 3
#OUTPUT
runfile('C:/ip practical programs/p3.py', wdir='C:/ip practical programs')
Original list:
0 41
1 44
2 47
3 50
4 53
5 56
6 59
7 62
8 65
9 68
dtype: int64
updated list
0 48
1 41
2 54
3 47
4 60
5 53
6 66
7 59
8 72
9 65
dtype: int64
#PROGRAM 4
#DISPLAYING ROW & COLUMN LABELS, DATA TYPES OF
COLUMNS AND DIMENSIONS OF THE DATA FRAME
import pandas as pd
dic={"class":
["I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII"],"pass-
percentage":[100,100,100,100,100,100,100,100,100,98.6,100,99]}
result=pd.DataFrame(dic)
print(result)
print(result.dtypes)
print("Shape of the dataframe is:")
print(result.shape)
#PROGRAM 4
#OUTPUT
runfile('C:/ip practical programs/p4.py', wdir='C:/ip practical programs')
class pass-percentage
0 I 100.0
1 II 100.0
2 III 100.0
3 IV 100.0
4 V 100.0
5 VI 100.0
6 VII 100.0
7 VIII 100.0
8 IX 100.0
9 X 98.6
10 XI 100.0
11 XII 99.0
class object
pass-percentage float64
dtype: object
Shape of the dataframe is:
(12, 2)
#PROGRAM 5
#DATA SERIES USING SLICING
import pandas as pd
l=[1,2,3,4,5,6,7,8,9]
print("Enter for slicing without step value")
print("Enter for slicing with step value")
while True:
ch= int(input("Enter your choice:"))
if ch==1:
s=pd.Series(l)
print(s)
print("Slicing without step value")
a=int(input("Enter the start value:"))
b=int(input("Enter the end value:"))
print(s[a:b])
elif ch==2:
s=pd.Series(l)
print("Slicing with step value")
a=int(input("Enter the start value:"))
b=int(input("Enter the end value:"))
c=int(input("Enter the step value:"))
print(s[a:b:c])
else:
print("INVALID")
ans=input("Do you want to continue:")
if ans!="yes":
break
#PROGRAM 5
#OUTPUT
runfile('C:/ip practical programs/program5.py', wdir='C:/ip practical programs')
Enter for slicing without step value
Enter for slicing with step value
Enter your choice:1
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
dtype: int64
Slicing without step value
Enter the start value:2
Enter the end value:6
2 3
3 4
4 5
5 6
dtype: int64
Do you want to continue:yes
Enter your choice:2
Slicing with step value
Enter the start value:3
Enter the end value:8
Enter the step value:2
3 4
5 6
7 8
dtype: int64
Do you want to continue:n
#PROGRAM 6
#CREATE A MARK LIST USING DATA SERIES
import pandas as pd
marks=pd.Series(data=[20,45,67,45,78,87,35,56])
print("Enter the option for displaying all the marks who scored above 75")
print("Enter the option for displaying the marks who scored below 75")
print("Enter the option for displaying the marks in ascending order")
print("Enter the option for displaying the marks in descending order")
while True:
ch=int(input("Enter your choice:"))
if ch==1:
print("The marks who scored above 75")
print(marks[marks>75])
if ch==2:
print("The marks who scored below 75")
print(marks[marks<75])
if ch==3:
print("The marks in ascending order")
print(marks.sort_values())
if ch==4:
print("The marks in descending order")
print(marks.sort_values(ascending=False))
else:
print("INVALID")
ans=input("Do you want to continue:")
if ans!="yes":
break
#PROGRAM 6
#OUTPUT
runfile('C:/ip practical programs/program6.py', wdir='C:/ip practical programs')
Enter the option for displaying all the marks who scored above 75
Enter the option for displaying the marks who scored below 75
Enter the option for displaying the marks in ascending order
Enter the option for displaying the marks in descending order
Enter your choice:1
The marks who scored above 75
4 78
5 87
dtype: int64
INVALID
Do you want to continue:yes
Enter your choice:2
The marks who scored below 75
0 20
1 45
2 67
3 45
6 35
7 56
dtype: int64
INVALID
Do you want to continue:yes
Enter your choice:3
The marks in ascending order
0 20
6 35
1 45
3 45
7 56
2 67
4 78
5 87
dtype: int64
INVALID
Do you want to continue:yes
Enter your choice:4
The marks in descending order
5 87
4 78
2 67
7 56
1 45
3 45
6 35
0 20
dtype: int64
Do you want to continue:n
#PROGRAM 7
#ITERATING OVER A DATAFRAME
import pandas as pd
states=pd.DataFrame({'POPULATION':
[120251,153242,152364,136548,102020],'HOSPITALS':
[1520,1200,1100,1212,1420],'SCHOOLS':
[1100,1200,1300,1100,1420],'DISTRICTS':
[20,30,14,20,15]},index=["TN","MP","KERALA","AP","DEL"])
print(states)
for (ri,rv) in states.iterrows():
print("ROW INDEX IS:",ri)
print("ROW VALUES ARE:")
print(rv)
print("**************************")
for (col_name,col_values) in states.items():
print("COLUMN NAME IS:",col_name)
print("COLUMN VALUES ARE:")
print(col_values)
print("**************************")
#PROGRAM 7
#OUTPUT
runfile('C:/ip practical programs/program7.py', wdir='C:/ip practical programs')
POPULATION HOSPITALS SCHOOLS DISTRICTS
TN 120251 1520 1100 20
MP 153242 1200 1200 30
KERALA 152364 1100 1300 14
AP 136548 1212 1100 20
DEL 102020 1420 1420 15
ROW INDEX IS: TN
ROW VALUES ARE:
POPULATION 120251
HOSPITALS 1520
SCHOOLS 1100
DISTRICTS 20
Name: TN, dtype: int64
**************************
ROW INDEX IS: MP
ROW VALUES ARE:
POPULATION 153242
HOSPITALS 1200
SCHOOLS 1200
DISTRICTS 30
Name: MP, dtype: int64
**************************
ROW INDEX IS: KERALA
ROW VALUES ARE:
POPULATION 152364
HOSPITALS 1100
SCHOOLS 1300
DISTRICTS 14
Name: KERALA, dtype: int64
**************************
ROW INDEX IS: AP
ROW VALUES ARE:
POPULATION 136548
HOSPITALS 1212
SCHOOLS 1100
DISTRICTS 20
Name: AP, dtype: int64
**************************
ROW INDEX IS: DEL
ROW VALUES ARE:
POPULATION 102020
HOSPITALS 1420
SCHOOLS 1420
DISTRICTS 15
Name: DEL, dtype: int64
**************************
COLUMN NAME IS: POPULATION
COLUMN VALUES ARE:
TN 120251
MP 153242
KERALA 152364
AP 136548
DEL 102020
Name: POPULATION, dtype: int64
**************************
COLUMN NAME IS: HOSPITALS
COLUMN VALUES ARE:
TN 1520
MP 1200
KERALA 1100
AP 1212
DEL 1420
Name: HOSPITALS, dtype: int64
**************************
COLUMN NAME IS: SCHOOLS
COLUMN VALUES ARE:
TN 1100
MP 1200
KERALA 1300
AP 1100
DEL 1420
Name: SCHOOLS, dtype: int64
**************************
COLUMN NAME IS: DISTRICTS
COLUMN VALUES ARE:
TN 20
MP 30
KERALA 14
AP 20
DEL 15
Name: DISTRICTS, dtype: int64
**************************
#PROGRAM 11
#ARITHMETIC OPERATIONS USING DATA FRAME
Aim
Write a menu driven program to create a two Data Frame and perform arithmetic
operations.
Source code:
import pandas as pd
d1={"English":[60,65,67],"Economics":[68,87,57],"Maths":[78,89,99],"IP":[78,86,69]}
d2={"English":[70,65,87],"Economics":[88,97,67],"Maths":[58,89,100],"IP":[68,86,99]}
data1=pd.DataFrame(d1)
print(data1)
data2=pd.DataFrame(d2)
print(data2)
print("enter 1 for addition")
print("enter 2 for subtraction")
print("enter 3 for multiplication")
print("enter 4 for division")
while True:
ch=int(input("enter your choice:"))
if ch==1:
add=data1.add(data2)
print(add)
if ch==2:
sub=data1.sub(data2)
print(sub)
if ch==3:
mul=data1.mul(data2)
print(mul)
if ch==4:
div=data1.div(data2)
print(div)
else:
print("Invalid")
a=input("Do you want to continue:")
if a!="yes":
break
#Output
runfile('C:/ip practical programs/program11.py', wdir='C:/ip practical programs')
English Economics Maths IP
0 60 68 78 78
1 65 87 89 86
2 67 57 99 69
English Economics Maths IP
0 70 88 58 68
1 65 97 89 86
2 87 67 100 99
enter 1 for addition
enter 2 for subtraction
enter 3 for multiplication
enter 4 for division
enter your choice:1
English Economics Maths IP
0 130 156 136 146
1 130 184 178 172
2 154 124 199 168
Invalid
Do you want to continue:yes
enter your choice:2
English Economics Maths IP
0 -10 -20 20 10
1 0 -10 0 0
2 -20 -10 -1 -30
Invalid
Do you want to continue:yes
enter your choice:3
English Economics Maths IP
0 4200 5984 4524 5304
1 4225 8439 7921 7396
2 5829 3819 9900 6831
Invalid
Do you want to continue:yes
enter your choice:4
English Economics Maths IP
0 0.857143 0.772727 1.344828 1.147059
1 1.000000 0.896907 1.000000 1.000000
2 0.770115 0.850746 0.990000 0.696970
Do you want to continue:n