user9454050
user9454050

Reputation: 141

resample pandas dataframe at to arbitrary number

I have a loop in which a new data frame is populated with values during each step. The number of rows in the new dataframe is different for each step in the loop. At the end of the loop, I want to compare the dataframes and in order to do so, they all need to be the same length. Is there a way I can resample the dataframe at each step to an arbitrary number (eg. 5618) of rows?

Upvotes: 0

Views: 301

Answers (1)

KWx
KWx

Reputation: 310

If your dataframe is too small by N rows, then you can randomly sample N rows with replacement and add the rows on to the end of your original dataframe. If your dataframe is too big, then sample the desired number from the original dataframe .

if len(df) <5618:
    df1 = df.sample(n=5618-len(df),replace=True)
    df = pd.concat([df,df1])
if len(df) > 5618:
    df = df.sample(n=5618)

Upvotes: 1

Related Questions