Python Practice › Pandas Programs › Program 55

Shuffle rows

EasyPandas Programs · 55 of 95

Write a pandas program to shuffle rows.

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

Approach

  1. sample(frac=1); seed with random_state.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.sample(frac=1, random_state=3))
Output
    name  age
3  Klaus   33
1   John   27
0   Alex   30
2  Peter   29

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Leave a Comment