DataFrame 逐行附加到 DataFrame 并在条件匹配时重置

DataFrame append to DataFrame row by row and reset if condition is matched

我有一个 DataFrame,我想通过逐行添加将其分成许多 DataFrame,直到 DataFrame 的列 Score 的总和大于 50,000。一旦满足该条件,那么我想要一个新的切片开始。

下面是一个示例:

累加 Score,floor 除以 50,000,然后向上移动一个单元格(因为您希望每个组 > 50,000 而不是 < 50,000)。

import pandas as pd
import numpy as np

# Generating DataFrame with random data
df = pd.DataFrame(np.random.randint(1,60000,15))

# Creating new column that's a cumulative sum with each
# value floor divided by 50000
df['groups'] = df[0].cumsum() // 50000

# Values shifted up one and missing values filled with the maximum value
# so that values at the bottom are included in the last DataFrame slice
df.groups = df.groups.shift(-1, fill_value=df.groups.max())

然后根据 ,您可以在列表推导中使用 pandas.DataFrame.groupby 到 return 拆分数据帧列表。

df_list = [df_slice for _, df_slice in df.groupby(['groups'])]