Dataframe in Pandas
Dataframe in Pandas
Here, Names of the students are row labels and term names (UT1, Half
Yearly, UT2 and Final) are the column labels. Answer the following questions
based on the above dataframe:
a. Change the row labels from student name to roll numbers from 1 to 6.
b. Change the column labels to Term1, Term2, Term3, Term4.
c. Add a new column Grade with values ‘A’, ‘A’,’B’,’A’,’C’, ‘B’
d. Add a new row for the student with row label=7 and marks equal to
49, 56, 75,58 and grade=b.
e. Delete the first row
f. Delete the third column
g. Display 2nd row with all columns
h. Display students who have scored more than 50 in Final exam
i. Check students who have grade as A
j. Display marks in Half Yearly and Final of all students
k. Display marks of students from Mansi to Ankita
l. Display marks of Mansi to Ankita in UT1 and UT2
m. Display marks of Kanika and Ankita in Half Yearly and Final
n. Display first 3 records
o. Display last four records
Q3. Write a Python program to create the following dataframe DOCTOR using
the index values as 10,20,30,40, 50, 60, 70.
a. Flight_Fare [Flight_Fare.index>1]
b. Flight_Fare [( Flight_Fare .FARE>=4000)&( Flight_Fare
.FARE<=9000)]
c. Flight_Fare [( Flight_Fare .FL_NO== "IC701")| ( Flight_Fare
.FL_NO== "AM501")| ( Flight_Fare .FL_NO== " IC302")]
d. Flight_Fare [( Flight_Fare .FARE>=4000)&( Flight_Fare
.FARE<=9000)][[ "FL_NO", "FARE"]]
e. Flight_Fare [2:4]
f. Flight_Fare [:4]
g. Flight_Fare [::3]
h. Flight_Fare [:: -3]
i. Flight_Fare [3:]
j. Flight_Fare.loc[1:4,'FL_NO':'FARE']
k. Flight_Fare.loc[1:4,['FL_NO','FARE']]
l. Flight_Fare.iloc[[0,2,4]]
m. Flight_Fare.iloc[:,1:3]
n. Flight_Fare.iloc[1:2,1:3]
o. Flight_Fare.loc[1:3]
p. Flight_Fare.loc[:,'FL_NO':'FARE']
q. Flight_Fare ["Tax%"] = [10,8,9,5,7]
r. Flight_Fare.loc[5]=[ "MC101", "DECCAN AIRLINES", "3500",”10”]
s. Flight_Fare.loc [:,"Disc%"] = [2,3,2,4,2]
t. Flight_Fare =Flight_Fare.drop("Tax%", axis=1)
u. Flight_Fare =Flight_Fare.drop(4, axis=0)
v. Flight_Fare =Flight_Fare.drop([1,4] , axis=0)
w. Flight_Fare.loc[2]
x. Flight_Fare.loc[:,"FL_NO"]
y. Flight_Fare ["FARE"]>=6000
Solutions
Q1
(i) import pandas as pd
f=pd.DataFrame(a)
print(f)
OUTPUT
import pandas as pd
m=[{'item':'charger','cost':500,'discount':'5%'},
{'item':'books','cost':750},
{'item':'clock','cost':1200,'discount':'10%'}]
df=pd.DataFrame(m)
print(df)
(ii)
OUTPUT
0 charger 500 5%
(iii) import pandas as pd
result = {'2015':pd.Series(('78%','56%','90%','79%','60%')),
'2016':pd.Series(('64%','85%','72%','56%','48%')),
'2017':pd.Series(('45%','66%','78%','88%','73%')),
'2018':pd.Series(('70%','56%','38%','89%','94%')),
'2019':pd.Series(('66%','78%','58%','90%','83%'))}
rs=pd.DataFrame(result)
rs.index=[1,2,3,4,5]
print(rs)
OUTPUT
2015 2016 2017 2018 2019
import pandas as pd
'efiel tower']),
'year':pd.Series([1193,1572,1683,1630,1887]),
pm=pd.DataFrame(mn)
print(pm)
(iv)
OUTPUT
(v) import pandas as pd
con={'country':pd.Series(['India','Australia', 'China']),
pp=pd.DataFrame(con)
print(pp)
OUTPUT
a=[[58,83,49,89],[86,67,87,90],[92,78,45,56],[52,84,55,78],[93,75,87,69],
[98,79,88,96]]
m=pd.DataFrame(a,index=['sharad','mansi','kanika','ramesh','ankita','pranay'],
columns=['ut1','halfyearly','ut2','final'])
print(m)
OUTPUT
ut1 halfyearly ut2 final
(a) m=m.rename({'sharad':1,'mansi':2,'kanika':3,'ramesh':4,'ankita':5,'pranay':6},
axis="index")
print(m)
OUTPUT
axis="columns")
print(m)
o\p
(c) m['Grade']=['a','b','b','a','a','b']
print(m)
OUTPUT
m.loc[7]=[49,56,75,58, 'b']
print(m)
OUTPUT
(e) m.drop(1,axis=0)
OUTPUT
term1 term2 term3 term4 Grade
m.drop('term3',axis=1)
OUTPUT
(g) m.loc[2]
OUTPUT
term1 92
term2 78
term3 45
term4 56
Grade b
m['term4']>50
OUTPUT
1 True
2 True
3 True
(h)
4 True
5 True
6 True
7 True
bm.loc[:,'Grade']=='a'
OUTPUT
0 True
1 False
2 False
(i)
3 True
4 True
5 False
6 False
m.loc[:,["term2","term4"]]
or
m[["term2","term4"]]
OUTPUT
term2 term4
2 67 90
3 78 56
4 84 78
5 75 69
6 79 96
7 56 58
m.loc['2':'5']
OUTPUT
OUTPUT
term1 term2
2 86 67
3 92 78
4 52 84
5 93 75
m.loc[['3','5'],['term2','term4']]
OUTPUT
(m)
term2 term4
3 78 56
OUTPUT
OUTPUT
a={'ID':[101,102,103,104,105,106,107],
'NAME':['JOHN','SMITH','GEORGE','LARA','K GEORGE','JOHNSON','LUCY'],
'DEPT':['ENT','ORTHOPEDIC','CARDIOLOGY','SKIN','MEDICINE','ORTHOPEDIC','ENT'],
'EXPERIENCE':[12,5,10,3,9,10,3]}
print(df1)
OUTPUT
OUTPUT
ID 104
(a)
NAME LARA
DEPT SKIN
EXPERIENCE 3
OUTPUT
OUTPUT
10 JOHN
20 SMITH
40 LARA
60 JOHNSON
70 LUCY
OUTPUT
OUTPUT
OUTPUT
OUTPUT
(g)
DEPT EXPERIENCE
20 ORTHOPEDIC 5
df1.loc[20:60]
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
(c)
OUTPUT
FL_NO FARE
(d)
0 IC701 6500
3 IC899 8300
4 IC302 4300
Flight_Fare [2:4]
OUTPUT
(e)
FL_NO AIRLINES FARE
OUTPUT
OUTPUT
(g)
FL_NO AIRLINES FARE
OUTPUT
(h)
FL_NO AIRLINES FARE
OUTPUT
(i)
FL_NO AIRLINES FARE
OUTPUT
OUTPUT
FL_NO FARE
(k)
1 MU499 9400
2 AM501 13400
3 IC899 8300
4 IC302 4300
Flight_Fare.iloc[[0,2,4]]
OUTPUT
OUTPUT
OUTPUT
AIRLINES FARE
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
Flight_Fare =Flight_Fare.drop("Tax%", axis=1)
OUTPUT
OUTPUT
OUTPUT
FL_NO AM501
FARE 13400
OUTPUT
0 IC701
1 MU499
(x)
2 AM501
3 IC899
4 IC302
Flight_Fare ["FARE"]>=6000
OUTPUT
0 True
1 True
(y)
2 True
3 True
4 False