问题:熊猫数据框中的随机行选择
有没有一种方法可以从Pandas的DataFrame中选择随机行。
在R中,使用汽车包装,有一个有用的功能some(x, n)
,它类似于head,但在此示例中,从x中随机选择10行。
我也看过切片文档,似乎没有什么等效的。
更新资料
现在使用版本20。有一个示例方法。
df.sample(n)
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)
回答 0
像这样吗
import random
def some(x, n):
return x.ix[random.sample(x.index, n)]
注:由于熊猫v0.20.0的,ix
已被弃用,赞成loc
基于标签索引。
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.
回答 1
随着pandas版本0.16.1
及更高版本,现在DataFrame.sample
内置了一个方法:
import pandas
df = pandas.DataFrame(pandas.np.random.random(100))
# Randomly sample 70% of your dataframe
df_percent = df.sample(frac=0.7)
# Randomly sample 7 elements from your dataframe
df_elements = df.sample(n=7)
对于上述两种方法,您都可以通过执行以下操作获得其余的行:
df_rest = df.loc[~df.index.isin(df_percent.index)]
With pandas version 0.16.1
and up, there is now a DataFrame.sample
method built-in:
import pandas
df = pandas.DataFrame(pandas.np.random.random(100))
# Randomly sample 70% of your dataframe
df_percent = df.sample(frac=0.7)
# Randomly sample 7 elements from your dataframe
df_elements = df.sample(n=7)
For either approach above, you can get the rest of the rows by doing:
df_rest = df.loc[~df.index.isin(df_percent.index)]
回答 2
从v0.20.0开始,您可以使用pd.DataFrame.sample
,它可用于返回固定数量的行或行百分比的随机样本:
df = df.sample(n=k) # k rows
df = df.sample(frac=k) # int(len(df.index) * k) rows
为了重现性,您可以指定一个整数random_state
,等效于使用np.ramdom.seed
。因此,不用设置,例如np.random.seed = 0
,您可以:
df = df.sample(n=k, random_state=0)
As of v0.20.0, you can use pd.DataFrame.sample
, which can be used to return a random sample of a fixed number rows, or a percentage of rows:
df = df.sample(n=k) # k rows
df = df.sample(frac=k) # int(len(df.index) * k) rows
For reproducibility, you can specify an integer random_state
, equivalent to using np.ramdom.seed
. So, instead of setting, for example, np.random.seed = 0
, you can:
df = df.sample(n=k, random_state=0)
回答 3
最好的方法是使用随机模块中的样本函数,
import numpy as np
import pandas as pd
from random import sample
# given data frame df
# create random index
rindex = np.array(sample(xrange(len(df)), 10))
# get 10 random rows from df
dfr = df.ix[rindex]
The best way to do this is with the sample function from the random module,
import numpy as np
import pandas as pd
from random import sample
# given data frame df
# create random index
rindex = np.array(sample(xrange(len(df)), 10))
# get 10 random rows from df
dfr = df.ix[rindex]
回答 4
实际上,这将为您提供重复的索引np.random.random_integers(0, len(df), N)
,其中的索引N
很大。
Actually this will give you repeated indices np.random.random_integers(0, len(df), N)
where N
is a large number.
回答 5
下面的行将从数据帧df的现有行总数中随机选择n个行,而不进行替换。
df=df.take(np.random.permutation(len(df))[:n])
Below line will randomly select n number of rows out of the total existing row numbers from the dataframe df without replacement.
df=df.take(np.random.permutation(len(df))[:n])