Q. A dictionary Grade contains the following data:


Grade = {'Name': [ 'Rashmi', 'Harsh', 'Ganesh', 'Priya', 'Vivek', 'Anita', 'Karthik'], 'Grade': ['A1', 'A2', 'B1', 'A1', 'B2', 'A2', 'A1']}

Write statements for the following:


(a) Create a dataframe called Gr.

(b) Find the output of Gr.iloc[0:5] and Gr[0:5]

(c) Add a column called Percentage with the following data: [92, 89, None, 95, 68, None, 93].

(d) Rearrange the columns as Name, Percentage and Grade.

(e) Drop the column (i.e., Grade) by name.

(f) Delete the 3rd and 5th rows.

(g) What will the following codes do?

(i) Gr.drop(0, axis = 0)

(ii) Gr.drop(0, axis = "index")

(ii) Gr.drop([0, 1, 2, 3],axis = 0).


Answer :-

(a) Gr = pd. DataFrame (Grade)
(b) Output for both the commands is the same:

    Name    Grade
0    Rashmi    A1
1    Harsh    A2
2    Ganesh    B1
3    Priya    A1
4    Vivek    B2


(c) Gr["Percentage"] = [92, 89, None, 95, 68, None, 93]
(d) Gr = Gr[['Name', 'Percentage', 'Grade']]
(e) Gr.drop('Grade', axis = 1)
(f) Gr.drop ([2, 4])
(g)
(i) First row will be dropped.
(ii) First row will be dropped.
(iii) First four rows will be dropped.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post