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.
JavaScript
x
2
1
df.sample(n)
2
Advertisement
Answer
Something like this?
JavaScript
1
5
1
import random
2
3
def some(x, n):
4
return x.ix[random.sample(x.index, n)]
5
Note: As of Pandas v0.20.0, ix
has been deprecated in favour of loc
for label based indexing.