![]() |
VOOZH | about |
Sorting rows in a Pandas DataFrame means rearranging rows based on the values of one or more columns. Pandas provides the sort_values() method to do this efficiently.
In this example, we sort movies by their release year in ascending order.
Movie Year 0 The Godfather 1972 2 Titanic 1997 1 Inception 2010
Explanation:
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, na_position='last')
Parameters:
Return Value: Returns a DataFrame with rows sorted by the given column(s).
Example 1: In this example, we sort student scores in Science from highest to lowest.
Name Science 1 Marsh 9 0 Simon 7 3 Selena 7 2 Alex 4
Explanation:
Example 2: This code sorts students first by Maths, then by English, both in ascending order.
Name Maths English 1 Marsh 5 4 2 Gaurav 6 7 0 Simon 8 7 3 Alex 9 6
Explanation:
Example 3: In this example, we sort by Science but display missing values (NaN) first.
Name Science 1 Marsh NaN 2 Alex 4.0 0 Simon 7.0 3 Selena 9.0
Explanation: