Question:

Aditya is working on a DataFrame named df. He has written the statement:

print(df.loc['S2'])

What will the above statement do?

Show Hint

The loc indexer works with labels. A single label passed to df.loc selects one row.
Updated On: Oct 20, 2026
  • Display the data of the row having label 'S2'.
  • Display the columns of the DataFrame.
  • Display the data type of the column having label 'S2'.
  • Display the index numbers of the DataFrame.
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: Recall what loc does:
In Pandas, the loc indexer selects data by label.
The form df.loc[label] picks the row whose index label equals the given label.

Step 2: Read the statement:
The statement is print(df.loc['S2']).
Here 'S2' is a row label in the index of df.
So Pandas picks the row labelled S2 and prints its values.

Step 3: Check with a real run:
Take df with the columns x and y and the row labels S1 and S2.
The code df.loc['S2'] prints the row for S2 with x = 2 and y = 4, along with the name S2.
So it shows a row, not a column.

Step 4: Why the other options are wrong:
Option 2 is wrong because the columns of a DataFrame are shown by df.columns.
Option 3 is wrong because a data type is shown by df.dtypes, and 'S2' is a row label here.
Option 4 is wrong because the index numbers are shown by df.index.

Final Answer:
The statement displays the data of the row having label 'S2'. This is option 1.
Was this answer helpful?
0
0