Python Practice › Pandas Programs › Program 55
Shuffle rows
Write a pandas program to shuffle rows.
Output
name age 3 Klaus 33 1 John 27 0 Alex 30 2 Peter 29
Approach
sample(frac=1); seed withrandom_state.
Solution
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.