redrubia
redrubia

Reputation: 2366

How to sample pandas DataFrame with replacement?

I have a DataFrame, size N. I need to sample it with S samples, with replacement where N < S.

def sampleDF(df, K): 
    return df.ix[np.random.randint(0, len(df), size=k)]

I return a new DF but it seems everything is filled with NaN. Am not sure what's going on!

Upvotes: 4

Views: 3672

Answers (1)

HYRY
HYRY

Reputation: 97281

use iloc[]:

df.iloc[np.random.randint(0, len(df), size=k)]

Upvotes: 5

Related Questions