嗨我有一个示例数据框如下:
第一组 | 第2组 | 条件 1 | 点 | 总容量 |
---|---|---|---|---|
A | 1 | 0 | 0.3 | 2 |
A | 1 | 1 | 0.5 | 2 |
A | 1 | 0 | 0.8 | 2 |
A | 1 | 0 | 0.2 | 2 |
A | 1 | 0 | 0.4 | 2 |
乙 | 2 | 0 | 0.6 | 4 |
乙 | 2 | 0 | 0.3 | 4 |
乙 | 2 | 1 | 0.1 | 4 |
乙 | 2 | 0 | 0.4 | 4 |
乙 | 3 | 0 | 0.5 | 4 |
乙 | 3 | 0 | 0.2 | 4 |
我正在尝试根据几个条件创建另一个值为 0 和 1 的列。 在第 1 组和第 2 组的每组中,每当 cond 1 = 1 时,我将对每组中所有剩余的点行求和,并检查点的总和是否小于或等于 total_capacity。
我尝试了以下代码,
df['new_col'] = 0
for index, row in df.iterrows():
if row['cond_1'] == 1:
group_1 = row['group_1']
group_2 = row['group_2']
total_capacity = row['total_capacity']
point = row['point']
remaining_point = df[(df['group_1'] == group_1) & (df['group_2'] == group_1) & (df.index >= index)]['point'].sum()
remaining_cap = remaining_point / 1.1
if (remaining_cap < total_capacity):
combined_df.at[index, 'new_col'] = 1
但是,我还需要计算点列的累计和,并在达到1.1时重置为0。我需要检查重置计数是否<= total_capacity. I am not sure how to include this condition in the above code. As per my logic, the new_col will be 1 in the first group combination beacuse (0.5 + 0.8 + 0.2 + 0.4) / 1.1 <= 2. But when we include the cumulative sum and the reset count, new_col will 0, because 0.5 + 0.8 is 1.3, so 0.5 = reset_count = 1, 0.8+0.2 = 1, reset_count =2 and 0.4 reset_count =3, finally, the condition reset_count 3 < 2 is not true, so the new_col = 0.
任何帮助表示赞赏。谢谢