Python Pandas 数据框中的随机行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15923826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:23:09  来源:igfitidea点击:

Random row selection in Pandas dataframe

pythonpandas

提问by John

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

有没有办法从 Pandas 的 DataFrame 中选择随机行。

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.

在 R 中,使用 car 包,有一个some(x, n)类似于 head的有用函数,但在此示例中,从 x 中随机选择 10 行。

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.

现在使用版本 20。有一个示例方法。

df.sample(n)

df.sample(n)

采纳答案by eumiro

Something like this?

像这样的东西?

import random

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

Note:As of Pandas v0.20.0, ixhas been deprecatedin favour of locfor label based indexing.

注意:从 Pandas v0.20.0 开始,ix已弃用,以支持loc基于标签的索引。

回答by rlmlr

Actually this will give you repeated indices np.random.random_integers(0, len(df), N)where Nis a large number.

实际上,这将为您提供重复的索引np.random.random_integers(0, len(df), N),其中N的数字很大。

回答by rlmlr

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]

回答by ryanjdillon

With pandas version 0.16.1and up, there is now a DataFrame.samplemethod built-in:

在 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)

For either approach above, you can get the rest of the rows by doing:

对于上述任何一种方法,您都可以通过执行以下操作来获取其余的行:

df_rest = df.loc[~df.index.isin(df_percent.index)]

回答by Mojgan Mazouchi

Below line will randomly select n number of rows out of the total existing row numbers from the dataframe df without replacement.

下面的行将从数据帧 df 的现有总行数中随机选择 n 行而不进行替换。

df=df.take(np.random.permutation(len(df))[:n])

df=df.take(np.random.permutation(len(df))[:n])

回答by jpp

sample

sample

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:

从 v0.20.0 开始,您可以使用pd.DataFrame.sample,它可用于返回固定数量行的随机样本,或行的百分比:

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:

为了重现性,您可以指定一个 integer random_state,相当于使用np.ramdom.seed。因此,例如np.random.seed = 0,您可以不设置 ,而是:

df = df.sample(n=k, random_state=0)