Skip to main contentSkip to solution

In any DataFrame 'df', if we want to perform sorting on a column name then which of the following is the correct syntax to do it?

Solution

✅ Correct Option: 4

DataFrame.sort_values() takes the column(s) to sort on through the parameter by. The parameter axis=0 (the default) sorts along rows, so the rows are reordered according to the values in the column 'Name'. Hence df.sort_values(by = ['Name'], axis = 0) is correct.


Options 1 and 2: with is not a parameter of sort_values(), so these raise an error.

Option 3: axis = 1 sorts the columns using the values of a row label, so by = ['Name'] (a column name) is wrong with it.

Keyboard Shortcuts

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