1. Home
  2. Data Science
  3. Pandas
  4. df.sort_values('score') is called on a float column…

Added 30 Nov -0001

df.sort_values('score') is called on a float column containing NaN. Where do the missing values end up?

  1. Scattered in place, because pandas leaves NaN rows where they originally stood
  2. The call raises, because sorting a column with NaN requires dropna first
  3. At the end by default, because na_position defaults to last in sort_values Correct answer
  4. At the beginning by default, because NaN sorts before every numeric value

Correct answer

C. At the end by default, because na_position defaults to last in sort_values

14 views

MCQSEXAM

Explanation

na_position controls where missing values land and defaults to 'last' in sort_values, independently of whether the sort is ascending. This differs from NumPy's sort, where NaN propagates to the end only because of its comparison semantics. Use df.sort_values('score', na_position='first') when missing records should lead the report.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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