1. Home
  2. Data Science
  3. Pandas
  4. Series.nlargest(2) is called on the values 9, 9, 9 and 5…

Added 30 Nov -0001

Series.nlargest(2) is called on the values 9, 9, 9 and 5. What does it return with default arguments?

  1. All three 9 values, because ties are always retained by nlargest
  2. Two 9 values, chosen by their index labels rather than by position
  3. Three rows followed by a truncation warning from pandas about ties
  4. Two of the tied 9 values, ordered by their position in the Series Correct answer

Correct answer

D. Two of the tied 9 values, ordered by their position in the Series

13 views

MCQSEXAM

Explanation

nlargest defaults to keep='first', so it returns exactly n rows and breaks ties by position in the Series; keep='all' returns every tied value and can therefore exceed n rows. Ties are decided on the values themselves, not on the index, so a sorted index does not influence the choice. The same keep argument appears on nsmallest and drop_duplicates.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs