Pandas 数据框中的随机行选择 [英] Random row selection in Pandas dataframe
本文介绍了Pandas 数据框中的随机行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从 Pandas 的 DataFrame 中选择随机行.
在 R 中,使用 car 包,有一个有用的函数 some(x, n)
,它类似于 head,但在本例中从 x 中随机选择 10 行.>
我还查看了切片文档,但似乎没有任何等效的内容.
更新
现在使用版本 20.有一个示例方法.
df.sample(n)
解决方案
类似的事情?
随机导入定义一些(x,n):返回 x.ix[random.sample(x.index, n)]
注意:从 Pandas v0.20.0 开始,ix
已被弃用,取而代之的是 loc
用于基于标签的索引.
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)
解决方案
Something like this?
import random
def some(x, n):
return x.ix[random.sample(x.index, n)]
Note: As of Pandas v0.20.0, ix
has been deprecated in favour of loc
for label based indexing.
这篇关于Pandas 数据框中的随机行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文