Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

I have the following DataFrame:

    Col1  Col2  Col3  Type

0      1     2     3     1

1      4     5     6     1

...

20     7     8     9     2

21    10    11    12     2

...

45    13    14    15     3

46    16    17    18     3

...

The DataFrame is read from a csv file. All rows which have Type 1 are on top, followed by the rows with Type 2, followed by the rows with Type 3, etc.

I would like to shuffle the order of the DataFrame's rows, so that all Type's are mixed. A possible result could be:

    Col1  Col2  Col3  Type

0      7     8     9     2

1     13    14    15     3

...

20     1     2     3     1

21    10    11    12     2

...

45     4     5     6     1

46    16    17    18     3

...

How can I achieve this?

1 Answer

0 votes
by (41.4k points)

Use sklearn for this

from sklearn.utils import shuffle

df = shuffle(df)

Shuffle arrays or sparse matrices in a consistent way

Related questions

Browse Categories

...