What is the difference between df.loc[2] and df.iloc[2]?
Correct answer
C. loc uses the index label 2; iloc uses position 2
14 views
C. loc uses the index label 2; iloc uses position 2
14 views
loc is label-based and matches the value stored in the index, while iloc is positional and counts from zero regardless of labels. When the index is the default RangeIndex starting at 0 the results coincide, which hides the difference until data is filtered or sorted and the index becomes non-contiguous. Both accept slice notation, but loc slices include the endpoint whereas iloc slices exclude it.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is C? Ask here and get an explanation.