pandas
DataFrame
data manipulation
shuffle
Python

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 is a standard pandas operation in data cleaning, exploratory analysis, and machine learning workflows. The goal is simple: return the same rows in a random order while preserving the column values attached to each row.

The Standard Way to Shuffle Rows

Pandas provides a built-in method for this through sample. To shuffle the full DataFrame, request a fraction of 1:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Ava", "Ben", "Cara", "Drew"],
6        "score": [88, 92, 75, 81],
7    }
8)
9
10shuffled = df.sample(frac=1, random_state=42).reset_index(drop=True)
11print(shuffled)

frac=1 means "return all rows." The row order changes, but each row remains intact. reset_index(drop=True) is optional, though it is usually useful when you want a clean 0, 1, 2 index after shuffling.

Why random_state Matters

Without random_state, you get a different order every time the code runs. That is good for true randomness, but inconvenient for testing, debugging, and repeatable training pipelines.

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

By setting a seed, you make the shuffle reproducible. This is especially important when you need a stable preprocessing pipeline for experiments.

Keeping or Resetting the Original Index

When you shuffle a DataFrame, pandas preserves the original index unless you change it. That behavior is useful if the index carries meaning, such as a database key or row identifier.

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

If you only care about row order and not the previous index labels, call reset_index(drop=True) afterward. This avoids confusion when someone reads the output and sees shuffled rows with the old index values.

Shuffling Before Splitting Data

One common use case is preparing a dataset before splitting it into training and testing sets. If the original file is ordered by date, label, or source, taking the first portion as training data can bias the model. Shuffling first makes the split more representative.

python
shuffled = df.sample(frac=1, random_state=42).reset_index(drop=True)
train = shuffled.iloc[:3]
test = shuffled.iloc[3:]

For production machine learning pipelines, you might use train_test_split from scikit-learn instead, but understanding the underlying shuffle is still useful.

Shuffling Large DataFrames

For most workloads, sample(frac=1) is the right answer. It is concise and readable. On extremely large DataFrames, though, any full shuffle can be expensive because pandas has to create a new ordering of the rows. If memory is tight, consider whether you need a full shuffle or only a random subset.

For example, if your real goal is inspection rather than full randomization, this is cheaper:

python
preview = df.sample(n=100, random_state=42)

That returns a random subset instead of reordering the entire table.

Common Pitfalls

The most common mistake is forgetting reset_index(drop=True) and then wondering why the shuffled DataFrame still shows the old row numbers. That is not an error; pandas is preserving the original index by design.

Another pitfall is omitting random_state in code that needs reproducibility. If a unit test depends on a specific order, leaving the seed unset will make the test flaky.

Be careful not to confuse shuffling with sorting. sample(frac=1) randomizes the order. sort_values orders rows by a column and is deterministic unless the values tie.

Finally, remember that sample returns a new DataFrame. If you want to keep the shuffled result, assign it back to a variable rather than expecting the original df to change in place.

Summary

  • Use df.sample(frac=1) to shuffle all rows in a pandas DataFrame.
  • Add random_state when you need reproducible results.
  • Use reset_index(drop=True) if you want a fresh sequential index after shuffling.
  • Keep the original index only when it carries meaningful row identity.
  • For large datasets, consider whether a random subset is enough instead of a full shuffle.

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.