python - 将 pandas 数据帧重新采样到任意数量
问题描述
我有一个循环,其中在每个步骤中都用值填充了一个新的数据框。对于循环中的每个步骤,新数据框中的行数是不同的。在循环结束时,我想比较数据帧,为此,它们都需要具有相同的长度。有没有办法可以在每一步将数据帧重新采样为任意数量(例如 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. 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)
推荐阅读
- django - 初始本地 Django mysql 数据库
- java - 如何修复从内部类引用的局部变量必须是最终或有效的最终错误
- java - 将 XSSFWorkbook 转换为 SXSSFWorkbook APACHE POI 时出现问题
- javascript - btn-group-justify 新的引导程序等效项。如何?
- gephi - Gephi 中的边缘厚度
- javascript - 如何从 JavaScript 中获取复制的文本
- python - Python中的UTCNow偏移
- winapi - 是否所有在 Windows 上运行的应用程序都使用 Windows API?
- php - 正则表达式不知道 URL 何时结束
- python - 海龟图形:重复正方形问题