CBSE Class 12 Informatics Practices Question 33 of 44

Data Handling using Pandas — Question 34

Back to all questions
34
Question

Question 31

Create a dataframe ‘Student’ from two series—Name and Grade, Name and Marks of five students.

(a) Display the first three records from student dataframe.

(b) Display the last two records from student dataframe.

Answer

The Student DataFrame is created as :

import pandas as pd

Name = ['John', 'Anna', 'Peter', 'Linda', 'Bob']
Grade = ['A', 'B', 'A+', 'C', 'B+']
Marks = [90, 80, 95, 70, 85]
S1 = pd.Series(Grade, index = Name) 
S2 = pd.Series(Marks, index = Name) 

Student = pd.DataFrame({'Grade' : S1, 'Marks' : S2 })
print(Student)
Output
      Grade  Marks
John      A     90
Anna      B     80
Peter    A+     95
Linda     C     70
Bob      B+     85

(a)

print(Student.head(3))
Output
      Grade  Marks
John      A     90
Anna      B     80
Peter    A+     95

(b)

print(Student.tail(2))
Output
      Grade  Marks
Linda     C     70
Bob      B+     85