Skip to content
Question
Q.

Consider the DataFrame Doctor shown below:

DIDNameDepartmentFee
0101Dr. JoeENT1500
1102Dr. SalmaUROLOGY1600
2103Dr. JeetORTHO1550
3104Dr. NehaENT1200
4105Dr. VikramORTHO1700

Write suitable Python statements for the following:

  1. To print the last three rows of the DataFrame Doctor.
  2. To display the names of all doctors.
  3. To add a new column 'Discount' with value of 200 for all doctors.
  4. To display rows with index 2 and 3.
  5. To delete the column 'Department'.
CBSECBSE Class XII Board 2025Subjective· 5mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Pandas DataFrames are fundamental for data manipulation in Python; operations like viewing specific rows, selecting columns, adding new columns, and deleting columns are performed using intuitive methods like .tail(), column indexing, and .drop().

In the realm of data analysis and manipulation using Python, the Pandas library is indispensable, and its core structure, the DataFrame, acts much like a sophisticated spreadsheet or a SQL table. A DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns). It's designed to handle structured data efficiently, making it a cornerstone for tasks ranging from data cleaning to complex statistical analysis.

The Doctor DataFrame presented is a perfect example of how data about entities (doctors, in this case) can be organized. Each row represents a unique doctor, and each column holds a specific attribute, such as their ID, Name, Department, or Fee. Understanding how to interact with and modify such a DataFrame is crucial for any data-driven task. Let's explore the Python statements required for the given operations.

import pandas as pd

# First, let's recreate the DataFrame Doctor for context
data = {
    'DID': [101, 102, 103, 104, 105],
    'Name': ['Dr. Joe', 'Dr. Salma', 'Dr. Jeet', 'Dr. Neha', 'Dr. Vikram'],
    'Department': ['ENT', 'UROLOGY', 'ORTHO', 'ENT', 'ORTHO'],
    'Fee': [1500, 1600, 1550, 1200, 1700]
}
Doctor = pd.DataFrame(data)
print("Original DataFrame Doctor:")
print(Doctor)
print("-" * 30)

(i) To print the last three rows of the DataFrame Doctor.

To view the concluding entries of a DataFrame, Pandas provides the .tail() method. This method is particularly useful when you have a large dataset and want to quickly inspect the most recent additions or the structure of the data at its end without printing the entire DataFrame. By default, .tail() shows the last five rows, but you can specify any number of rows as an argument.

# Python statement to print the last three rows
print("Last three rows of Doctor DataFrame:")
print(Doctor.tail(3))
print("-" * 30)

(ii) To display the names of all doctors.

When you need to access a specific column from a DataFrame, you can treat the DataFrame much like a dictionary, using the column name as a key. This operation extracts the column as a Pandas Series, which is a one-dimensional labeled array capable of holding any data type.

# Python statement to display the names of all doctors
print("Names of all doctors:")
print(Doctor['Name'])
print("-" * 30)

(iii) To add a new column 'Discount' with value of 200 for all doctors.

Adding a new column to a DataFrame is straightforward. You simply assign a value to a new column name using the square bracket notation. If you assign a single scalar value, Pandas automatically "broadcasts" this value to every row in the new column, ensuring that all entries in the 'Discount' column will be 200.

# Python statement to add a new column 'Discount'
Doctor['Discount'] = 200
print("DataFrame Doctor after adding 'Discount' column:")
print(Doctor)
print("-" * 30)
Note

When adding a new column, if you assign a list or Series, its length must match the number of rows in the DataFrame. Assigning a single scalar value, as done here, is a convenient way to populate the entire new column with that value.

(iv) To display rows with index 2 and 3. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.