Skip to main contentSkip to solution

Which function is used to sort a Pandas DataFrame in descending order based on a column?

Solution

✅ Correct Option: 1

DataFrame.sort_values() sorts a DataFrame by the values of one or more columns. The by argument names the column, and ascending=False gives descending order. The default is ascending=True.

Example: df.sort_values(by='Marks', ascending=False) puts the highest marks first.


Option 2: df.sort() with order='desc' is the old pandas way. It was deprecated and later removed, and sort_values() replaced it.

Options 3 and 4: order_by is an SQL/Django idea, and sort_data is not a pandas method. Neither exists in pandas.

sort_index() sorts by the row index, while sort_values() sorts by column values.

Keyboard Shortcuts

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