Sorting
sort_values sorts by columns and sort_index sorts by labels.
Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
print(df.sort_values("score", ascending=False)[["name", "score"]])
print(df.sort_values(["country", "score"])["name"].tolist())Result
name score 0 Ada 92 2 Amina 85 1 Kofi 78 3 Tunde 71 ['Kofi', 'Tunde', 'Amina', 'Ada']
Exercise
Sort by a column.
df.sort_values()
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.