🤜🤛 Aporia partners with Google Cloud to bring reliability and security to AI Agents  - Read more

Back to Blog
How-To

How to Shuffle DataFrame Rows

Aporia Team Aporia Team 1 min read Sep 06, 2022

Shuffling rows in a DataFrame means changing the order of rows. In this short how-to article, we will learn how to do this operation in Pandas DataFrames.

How to Shuffle DataFrame Rows in Pandas

Pandas

We can use the sample method, which returns a randomly selected sample from a DataFrame. If we make the size of the sample the same as the original DataFrame, the resulting sample will be the shuffled version of the original one.

# with n parameter
df = df.sample(n=len(df))

# with frac parameter
df = df.sample(frac=1)

The frac and n parameters define the size of the sample.

This question is also being asked as:

  • Pandas – how do you randomize the rows of a DataFrame

People have also asked for:

Rate this article

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Slack

On this page

Blog
Building a RAG app?

Consider AI Guardrails to get to production faster

Learn more
Table of Contents

Related Articles