Question:

Consider the DataFrame Doctor shown below:

Write suitable Python statements for the following:
(i) To print the last three rows of the DataFrame Doctor.
(ii) To display the names of all doctors.
(iii) To add a new column 'Discount' with value of 200 for all doctors.
(iv) To display rows with index 2 and 3.
(v) To delete the column 'Department'.
 

Show Hint

Use tail() for last rows, bracket notation for columns, loc[] for specific rows, and drop() to remove columns.
Updated On: Jul 14, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

(i) Print the last three rows of the DataFrame:
To view the last three rows, use the tail() function.
print(Doctor.tail(3))

Explanation: The tail() method displays the last n rows. Here, n is 3.
(ii) Display the names of all doctors:
To display only the 'Name' column, use:
print(Doctor['Name'])

Explanation: This selects only the 'Name' column and prints it.
(iii) Add a new column 'Discount':
To add a new column with the same value for all rows, use:
Doctor['Discount'] = 200

Explanation: This creates a new column named 'Discount' and assigns 200 to every row.
(iv) Display rows with index 2 and 3:
To select specific rows by index, use the loc indexer with a list of index labels.
print(Doctor.loc[[2, 3]])

Explanation: The loc method is label-based indexing for rows.
(v) Delete the column 'Department':
To drop a column from a DataFrame, use drop with axis=1.
Doctor = Doctor.drop('Department', axis=1)

Explanation: The drop() function removes the specified column and updates the DataFrame.
Was this answer helpful?
0
0

Top Questions on Python Libraries

View More Questions

Questions Asked in CBSE CLASS XII exam

View More Questions