简体   繁体   English

将熊猫数据框重新采样到任意数量

[英]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? 有没有一种方法可以在每个步骤将数据帧重新采样为任意数量的行(例如5618)?

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. 如果您的数据框太小了N行,那么您可以通过替换随机采样N行,并将这些行添加到原始数据框的末尾。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM