rshar
rshar

Reputation: 1477

Randomly select n rows from a dataframe t times?

How can I randomly select n rows from a dataframe t times?

For example: I have a dataframe df with 10 rows and I want to get 3 rows a time and repeat it 10 time.

a   value
n1  0.1
n2  0.2
n3  0.3
n4  0.4
n5  0.5
n6  0.6
n7  0.7
n8  0.8
n9  0.9
n10 1
Output
n1  0.1
n2  0.2
n3  0.3

n3  0.3
n5  0.5
n6  0.6

n1  0.1
n4  0.4
n3  0.3

n2  0.2
n4  0.4
n6  0.6

and so on...

In the end, I want to plot the score columns of these 10 small dataframes with another dataframe.

Till now, I have used the below function. But I do not know how to get the samples n times.

df_elements = df.sample(n=3)

Thanks

Upvotes: 1

Views: 378

Answers (2)

FBruzzesi
FBruzzesi

Reputation: 6495

The following will store n dataframes in a dictionary so that you can access them:

# Initialize dictionary
samples={}

# Store n samples
for i in range(1,n+1):
    samples[i] = df.sample(frac=0.3)

In this way you will be able to access the i-th sample by samples[i]

Upvotes: 0

jezrael
jezrael

Reputation: 863196

Use list comprehension:

df_elements = [x.sample(n=3) for i in range(10)]

If want join them together use concat:

df2 = pd.concat(df_elements, ignore_index=True)

Upvotes: 1

Related Questions