Label Indexing Wrsht-1 (With Solutions)
Label Indexing Wrsht-1 (With Solutions)
12 LABEL INDEXING
Consider the given dataframe and answer the following questions:
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
104 sushma 12 B 50 90 69 Avg
Q1. Write a program to create the dataframe “Student”.
Ans:
import pandas as pd dict1={'names':
['sush','adarsh','ravi','manu','sushma'],
'class':[11,12,11,12,12],
'sec':['A','A','C','A','B'],
'phy':[34,40,56,67,50],
'chem':[78,90,50,65,90],
'eng':[50,55,67,68,69],
'Proj_rem':['Avg','Good','Good','Fair','Avg'] }
student=pd.DataFrame(dict1,index=[100,101,102,103,104])
print(student)
Q2. Write command to display the names and section of all students.
Ans:
>>> student[['names','sec']]
names sec
100 sush A
101 adarsh A
102 ravi C
103 manu A
104 sushma B
Q3. Write command to display the details of the students in the reverse
order of their indexes.
Ans:
>>> student[::-1]
names class sec phy chem eng Proj_rem 104 sushma12 B 5090 69Avg
103manu12 A 6765 68Fair
102ravi11 C 5650 67Good 101 adarsh12 A 4090 55Good
100sush11 A 3478 50Avg
Q4. Write command to display the first 4 records ( use all possible methods).
>>> student[:4]
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
OR
>>> student.loc[100:103]
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
OR
>>> student.iloc[0:4]
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
OR
>>> student.head(4)
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
OR
>>> student.loc[[100,101,102,103]]
names class sec phy chem eng Proj_rem
100 sush 11 A 34 78 50 Avg
101 adarsh 12 A 40 90 55 Good
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
Q6. Write command to display the records with following row labels
102,104,100.
>>> student.loc[[102,104,100]]
names class sec phy chem eng Proj_rem
102 ravi 11 C 56 50 67 Good
104 sushma 12 B 50 90 69 Avg
100 sush 11 A 34 78 50 Avg
Q7. Write command to display the phy, chem and eng marks for the first two
students. (using iloc and loc)
>>>
student.loc[[100,101],['phy','chem','eng']]
phy chem eng
100 34 78 50
101 40 90 55
OR (by specifying individual default indexes of the rows and columns needed)
>>> student.iloc[[0,1],[3,4,5]]
phy chem eng
100 34 78 50
101 40 90 55
OR ( with use of Slicing concept with iloc)
>>> student.iloc[0:2,3:6]
phy chem eng
100 34 78 50
101 40 90 55
Q8. Write command to display the last 3 records.
>>> student.tail(3)
names class sec phy chem eng Proj_rem
102 ravi 11 C 56 50 67 Good
103 manu 12 A 67 65 68 Fair
104 sushma 12 B 50 90 69 Avg
Q10. Write command to display the name, section and Project name for the
record with third index.
>>> student.iloc[3,[0,2,6]]
names manu
sec A
Proj_rem Fair
Name: 103, dtype: object
Q11. Write command to display the names of first 4 records using iloc.
>>> student.iloc[0:4,0]
100 sush
101 adarsh
102 ravi
103 manu
Name: names, dtype: object
Q12. Write command to display the last 3 records in the reverse order of the
records.
>>> student.iloc[4:1:-1]
names class sec phy chem eng Proj_rem
104 sushma 12 B 50 90 69 Avg
103 manu 12 A 67 65 68 Fair
102 ravi 11 C 56 50 67 Good
Q13. Display the physics marks of student with row label 104
>>> student.loc[104,'phy']
50
OR
>>> student.iloc[4,3]
50