Which function is used to sort a Pandas DataFrame in descending order based on a column?
Which function is used to sort a Pandas DataFrame in descending order based on a column?
Solution
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.
Related questions:
2026: 13 May Shift 2
2025: 13 May Shift 2
2026: 23 May Shift 2
2025: 13 May Shift 2
2026: 24 May Shift 1