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