Data Science
Python
Pandas
Data Manipulation
Data Visualization

Shuffle DataFrame rows

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Shuffling DataFrame rows in Pandas is usually a one-liner, but the right version depends on whether you want reproducibility, whether you want to keep the original index, and whether the data has an ordering that must not be randomized. For most tabular machine-learning workflows, sample(frac=1) is the standard approach.

Core Sections

Use sample(frac=1) for a full shuffle

frac=1 means “return one hundred percent of the rows,” and because sample draws rows in random order, the result is a shuffled DataFrame.

python
1import pandas as pd
2
3
4df = pd.DataFrame(
5    {
6        "id": [1, 2, 3, 4],
7        "label": ["a", "b", "c", "d"],
8    }
9)
10
11shuffled = df.sample(frac=1)
12print(shuffled)

This preserves the original row contents exactly. Only the order changes. That property is important in data preprocessing, because shuffling should never break the alignment between feature columns and target columns stored in the same row.

Decide what to do with the index

After shuffling, the original index values stay attached to their original rows. That is often useful because it preserves row identity. But sometimes you want a clean index from zero upward.

python
shuffled = df.sample(frac=1).reset_index(drop=True)
print(shuffled)

Use drop=True unless you specifically want the old index to become a column. Otherwise, you will end up with an extra index column that many people did not intend to keep.

Make the shuffle reproducible

For experiments, tests, and notebooks, deterministic shuffling is often more important than randomness itself. Pandas supports this with random_state.

python
shuffled = df.sample(frac=1, random_state=42).reset_index(drop=True)
print(shuffled)

Now every run gives the same row order. That is especially useful when you are comparing model results and do not want hidden randomness to change the train-test split behavior.

Shuffle only a subset when needed

You can also sample a fraction of the rows rather than all of them. This is not a full shuffle, but it is related and often used in data exploration.

python
subset = df.sample(frac=0.5, random_state=42)
print(subset)

If the goal is to randomize the full table, keep frac=1. If the goal is downsampling, change the fraction intentionally.

When not to shuffle

Some datasets should not be shuffled casually. Time-series data is the most common example. If row order encodes temporal meaning, random shuffling can create leakage from future observations into past modeling steps.

Grouped data has a similar issue. If several rows belong to the same user, device, or transaction family, shuffling rows alone may not be enough. You may need grouped splitting rather than row-level randomization.

Alternatives to sample

You can shuffle with NumPy permutations too, but sample is usually clearer for DataFrames.

python
1import numpy as np
2
3shuffled = df.iloc[np.random.permutation(len(df))].reset_index(drop=True)
4print(shuffled)

This works, but sample communicates intent better and includes random_state directly.

Common Pitfalls

  • Forgetting to reset the index when downstream code assumes a fresh sequential index after shuffling.
  • Resetting the index without drop=True and then accidentally keeping an unwanted index column.
  • Omitting random_state in experiments where reproducibility matters.
  • Shuffling time-series or grouped data even though the original row order carries important structure.
  • Confusing downsampling with shuffling by changing frac away from 1 when the goal was only to randomize order.

Summary

  • The standard Pandas way to shuffle rows is df.sample(frac=1).
  • Add reset_index(drop=True) when you want a clean new index.
  • Use random_state for reproducible random order.
  • Avoid row-level shuffling when the dataset has temporal or grouped dependencies.
  • 'sample is usually clearer than manual index permutation for this task.'

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.