0% found this document useful (0 votes)
7 views6 pages

Label Indexing Wrsht-1 (With Solutions)

The document provides a series of programming exercises related to data manipulation using pandas in Python, specifically focusing on creating and querying a DataFrame of student records. It includes tasks such as displaying specific columns, reversing records, slicing data, and retrieving specific entries based on conditions. Each question is followed by the corresponding code and output examples to illustrate the expected results.

Uploaded by

tilakschoolexam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Label Indexing Wrsht-1 (With Solutions)

The document provides a series of programming exercises related to data manipulation using pandas in Python, specifically focusing on creating and querying a DataFrame of student records. It includes tasks such as displaying specific columns, reversing records, slicing data, and retrieving specific entries based on conditions. Each question is followed by the corresponding code and output examples to illustrate the expected results.

Uploaded by

tilakschoolexam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

INFORMATICS PRACTICES CLASS

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

Q5. Write command to display all odd number records.


>>> student[1:10:2]
names class sec phy chem eng Proj_rem
101 adarsh 12 A 40 90 55 Good
103 manu 12 A 67 65 68 Fair

OR (use of slicing concept with iloc with starting index:ending index:stepvalue)


>>> student.iloc[1:10:2]
names class sec phy chem eng Proj_rem
101 adarsh 12 A 40 90 55 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

OR ( mention actual row labels of the records)


>>> student.loc[[102,103,104]]
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
>>> student.loc[4:2:-1]

OR ( by using range of row labels)


NOTE: Do remember .loc works by including the upper range too contrary
to .iloc)
>>> student.loc[102:104]
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
Q9. Write command to display the name, section and project remarks for
first three records.
>>> student.loc[[100,101,103],['names','sec','Proj_rem']]
names sec Proj_rem
100 sush A Avg
101 adarsh A Good
103 manu A Fair

OR (use of slice for specifying row range )


>>> student.loc[100:103,['names','sec','Proj_rem']]
names sec Proj_rem
100 sush A Avg
101 adarsh A Good
102 ravi C Good
103 manu A Fair

OR ( use of default indexes in slicing format with .iloc)


>>> student.iloc[0:3,[0,2,6]]
names sec Proj_rem
100 sush A Avg
101 adarsh A Good
102 ravi C Good

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

You might also like