🎉 AI Engineers: Join our webinar on Prompt Engineering for AI Agents. Register here >>

Aporia How to's

How to Shuffle DataFrame Rows?

1 min read
shuffle dataframe rows pandas

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


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:

Green Background

Control All your GenAI Apps in minutes