Skip to main contentSkip to solution

NameTestEnglishMathsHindiScience
Sumit167877588
Sumit276787282
Jyoti177836087
Jyoti269896865
Nidhi156786569
Nidhi267868991

Which of the following statements will sort the values of the above DataFrame 'df' by Name?

Solution

✅ Correct Option: 3

DataFrame.sort_values(by=..., axis=0) sorts the rows according to the values in the column given in by. Here by=['Name'] with axis=0 reorders the rows alphabetically by Name, giving Jyoti, Nidhi, then Sumit. The default is axis=0, so it can also be written df.sort_values(by='Name').


Option 1: axis=1 sorts the columns, not the rows, using the values in a row label. It does not sort the data by Name.

Options 2 and 4: df.sort() is not a valid method in current pandas. The correct method is sort_values(), and in is also a Python keyword, so it cannot be used as a parameter name.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question