Comprehension

Consider the DataFrame Scholarship shown below:

NameCourseAmount
0AnanyaB.Sc.62000
1KaranB.Tech67000
2SimranMCA64000
3RahulBCA70000
4PriyaBA69000

Answer the following questions:

Question: 1

Write Python code to create and display the given DataFrame using the Dictionary of Series method.

Show Hint

Make one pd.Series for each column, put them in a dictionary with the column names as keys and pass it to pd.DataFrame().
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Look at the target table:
It has the columns Name, Course and Amount with the default row labels 0 to 4.
The question wants a dictionary of Series, so the values of the dictionary must be Series and not plain lists.

Step 2: Write the code in a short form:
We can write each Series directly inside the dictionary. This avoids three extra variables.
import pandas as pd
Scholarship = pd.DataFrame({
    'Name': pd.Series(['Ananya', 'Karan', 'Simran', 'Rahul', 'Priya']),
    'Course': pd.Series(['B.Sc.', 'B.Tech', 'MCA', 'BCA', 'BA']),
    'Amount': pd.Series([62000, 67000, 64000, 70000, 69000])
})
print(Scholarship)


Step 3: Why the rows line up:
Each Series gets the default index 0, 1, 2, 3, 4. pandas matches rows with the same index label.
So Ananya, B.Sc. and 62000 land in row 0, and so on down to Priya, BA and 69000 in row 4.

Step 4: Check the output:
The program prints the five rows below, the same as the given DataFrame.
     Name  Course  Amount
0  Ananya   B.Sc.   62000
1   Karan  B.Tech   67000
2  Simran     MCA   64000
3   Rahul     BCA   70000
4   Priya      BA   69000


Final Answer:
pd.DataFrame({'Name': Series, 'Course': Series, 'Amount': Series}) followed by print(Scholarship).
Was this answer helpful?
0
Question: 2

Rename row indexes to ['a', 'b', 'c', 'd', 'e']

Show Hint

Assign a list of five labels to the index attribute of the DataFrame, or use rename() with a dictionary.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Choose between two ways:
We can set the index attribute directly, or call the rename() method with a dictionary that maps old labels to new labels.

Step 2: Use rename():
The old labels are 0, 1, 2, 3, 4 and the new labels are a, b, c, d, e. So the mapping is 0 to a, 1 to b, and so on.
Scholarship = Scholarship.rename(index={0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'})

rename() returns a new DataFrame. So we store it back in Scholarship.

Step 3: The shorter way:
Scholarship.index = ['a', 'b', 'c', 'd', 'e'] does the same work in one line and changes the DataFrame in place. Either line is correct.

Step 4: Result:
The row labels are a, b, c, d, e from the first row to the last row. The data in the columns stays the same.

Final Answer:
Scholarship.index = ['a', 'b', 'c', 'd', 'e']
Was this answer helpful?
0
Question: 3

Write Python statement to remove the last row of the given DataFrame

Show Hint

Use the drop() method with the label of the last row, which Scholarship.index[-1] gives.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Think about what 'last row' means:
The last row is the row for Priya (BA, 69000). It has the label 4, or the label e if the labels were renamed in the earlier part.

Step 2: Remove it with a known label:
With the labels renamed in part II, the last label is 'e'. We use drop() with that label.
Scholarship.drop('e', inplace=True)

inplace=True changes Scholarship itself, so there is no need to assign the result.

Step 3: A form that works for any labels:
If we do not want to remember the label, we can take the last one from the index.
Scholarship.drop(Scholarship.index[-1], inplace=True)

With the original labels 0 to 4 this is the same as Scholarship.drop(4, inplace=True).

Step 4: Result:
Four rows remain: Ananya, Karan, Simran and Rahul. Priya's row is gone.

Final Answer:
Scholarship.drop(Scholarship.index[-1], inplace=True)
Was this answer helpful?
0