Back

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

Is there a way to select random rows from a DataFrame in Pandas.

In R, using the car package, there is a useful function some(x, n) which is similar to head but selects, in this example, 10 rows at random from x.

I have also looked at the slicing documentation and there seems to be nothing equivalent.

Update

Now using version 20. There is a sample method.

df.sample(n)

1 Answer

0 votes
by (41.4k points)

This below code will work:



import random

def some(x, n):

    return x.ix[random.sample(x.
index, n)]

If you wish to Learn more about Pandas visit this Pandas Tutorial.

Related questions

Browse Categories

...