Python Practice › Pandas Programs › Program 43
Sort by age
Write a pandas program to sort by age in ascending order.
Output
name age 1 John 27 2 Peter 29 0 Alex 30 3 Klaus 33
Approach
sort_values("age").
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.sort_values("age"))Output
name age 1 John 27 2 Peter 29 0 Alex 30 3 Klaus 33
Learn the concept
New to this topic? Read the lesson, then come back.