Question #128   Submitted by Answiki on 08/27/2020 at 05:27:37 PM UTC

How to shuffle rows in a Pandas dataframe in Python?

Answer   Submitted by Answiki on 08/27/2020 at 05:30:09 PM UTC

In Python, to shuffle rows in a dataframe, use the .sample() method:

df.sample(frac=1)


If you wish to shuffle and reset the index, use:

df = df.sample(frac=1).reset_index(drop=True)

2 events in history
Answer by Answiki on 08/27/2020 at 05:30:09 PM

In Python, to shuffle rows in a dataframe, use the .sample() method:

df.sample(frac=1)


If you wish to shuffle and reset the index, use:

df = df.sample(frac=1).reset_index(drop=True)

Question by Answiki 08/27/2020 at 05:27:37 PM
How to shuffle rows in a Pandas dataframe in Python?

Icons proudly provided by Friconix.