Python Practice › Pandas Programs › Program 41

Sort by name

EasyPandas Programs · 41 of 95

Write a pandas program to sort by name in ascending order.

Advertisement
Output
    name  age
1   Alex   30
3   John   27
2  Klaus   33
0  Peter   29

Approach

  1. sort_values("name").

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs