根据条件 pandas 和 return 其他累计和重置累计和
reset cumulative sum base on condition pandas and return other cumulative sum
我有这个数据框 -
counter duration amount
0 1 0.08 1,235
1 2 0.36 1,170
2 3 1.04 1,222
3 4 0.81 1,207
4 5 3.99 1,109
5 6 1.20 1,261
6 7 4.24 1,068
7 8 3.07 1,098
8 9 2.08 1,215
9 10 4.09 1,043
10 11 2.95 1,176
11 12 3.96 1,038
12 13 3.95 1,119
13 14 3.92 1,074
14 15 3.91 1,076
15 16 1.50 1,224
16 17 3.65 962
17 18 3.85 1,039
18 19 3.82 1,062
19 20 3.34 917
我想根据以下逻辑创建另一个栏目:
对于每一行,我想计算 'duration' 的 运行 总和,但对于当前行下方的行(前导和非前导),它应该是 运行 总和落后)。
我想在 运行 总和达到 5 时停止计算 -> 当它达到 5 时,我想 return 'amount' 的 运行 总和(具有相同的逻辑).
例如,对于 'counter' 1,它应该取前 4 行 (0.08+0.36+1.04+0.81<5) 然后到 return 1,235+1,170+1,222+1,207=4834
对于 'counter' 2 它应该只需要 0.36 + 1.04 + 0.81<5 和 return 1,170+1,222+1,207=3599
感谢任何帮助!
让我们尝试使用循环构建您自己的逻辑
c = df.duration.values
v=df.amount.values
result = []
lim=5
check = []
for i in range(len(c)):
total = 0
value = 0
for x, y in zip(v[i:],c[i:]):
total += y
value += x
if total >= lim:
result.append(value-x)
print(total)
break
#result
#[4834, 3599, 2429, 2316, 1109, 1261, 1068, 1098, 1215, 1043, 1176, 1038, 1119, 1074, 1076, 1224, 962, 1039, 1062]
我会先遍历 2 列一次以获得它们的累计总和。
cum_amount = df['amount'].cumsum()
cum_duration = df['duration'].cumsum()
为结果准备好列表
results = []
然后循环遍历每个索引(相当于计数器)
for idx in cum_duration.index:
# keep only rows within `5` and the max. index is where the required numbers are located
wanted_idx = (cum_duration[cum_duration<5]).index.max()
# read those numbers with the wanted index
results.append({'idx': idx, 'cum_duration': cum_duration[wanted_idx], 'cum_amount': cum_amount[wanted_idx]})
# subtract the lag (we need only the leads not the lags)
cum_amount -= cum_amount[idx]
cum_duration -= cum_duration[idx]
最后在 DataFrame 中的结果。
pd.DataFrame(results)
idx cum_duration cum_amount
0 0 2.29 4834.0
1 1 2.21 3599.0
2 2 1.85 2429.0
3 3 4.80 2316.0
4 4 3.99 1109.0
5 5 1.20 1261.0
6 6 4.24 1068.0
7 7 3.07 1098.0
8 8 2.08 1215.0
9 9 4.09 1043.0
10 10 2.95 1176.0
11 11 3.96 1038.0
12 12 3.95 1119.0
13 13 3.92 1074.0
14 14 3.91 1076.0
15 15 1.50 1224.0
16 16 3.65 962.0
17 17 3.85 1039.0
18 18 3.82 1062.0
19 19 3.34 917.0
我有这个数据框 -
counter duration amount
0 1 0.08 1,235
1 2 0.36 1,170
2 3 1.04 1,222
3 4 0.81 1,207
4 5 3.99 1,109
5 6 1.20 1,261
6 7 4.24 1,068
7 8 3.07 1,098
8 9 2.08 1,215
9 10 4.09 1,043
10 11 2.95 1,176
11 12 3.96 1,038
12 13 3.95 1,119
13 14 3.92 1,074
14 15 3.91 1,076
15 16 1.50 1,224
16 17 3.65 962
17 18 3.85 1,039
18 19 3.82 1,062
19 20 3.34 917
我想根据以下逻辑创建另一个栏目:
对于每一行,我想计算 'duration' 的 运行 总和,但对于当前行下方的行(前导和非前导),它应该是 运行 总和落后)。 我想在 运行 总和达到 5 时停止计算 -> 当它达到 5 时,我想 return 'amount' 的 运行 总和(具有相同的逻辑).
例如,对于 'counter' 1,它应该取前 4 行 (0.08+0.36+1.04+0.81<5) 然后到 return 1,235+1,170+1,222+1,207=4834
对于 'counter' 2 它应该只需要 0.36 + 1.04 + 0.81<5 和 return 1,170+1,222+1,207=3599
感谢任何帮助!
让我们尝试使用循环构建您自己的逻辑
c = df.duration.values
v=df.amount.values
result = []
lim=5
check = []
for i in range(len(c)):
total = 0
value = 0
for x, y in zip(v[i:],c[i:]):
total += y
value += x
if total >= lim:
result.append(value-x)
print(total)
break
#result
#[4834, 3599, 2429, 2316, 1109, 1261, 1068, 1098, 1215, 1043, 1176, 1038, 1119, 1074, 1076, 1224, 962, 1039, 1062]
我会先遍历 2 列一次以获得它们的累计总和。
cum_amount = df['amount'].cumsum()
cum_duration = df['duration'].cumsum()
为结果准备好列表
results = []
然后循环遍历每个索引(相当于计数器)
for idx in cum_duration.index:
# keep only rows within `5` and the max. index is where the required numbers are located
wanted_idx = (cum_duration[cum_duration<5]).index.max()
# read those numbers with the wanted index
results.append({'idx': idx, 'cum_duration': cum_duration[wanted_idx], 'cum_amount': cum_amount[wanted_idx]})
# subtract the lag (we need only the leads not the lags)
cum_amount -= cum_amount[idx]
cum_duration -= cum_duration[idx]
最后在 DataFrame 中的结果。
pd.DataFrame(results)
idx cum_duration cum_amount
0 0 2.29 4834.0
1 1 2.21 3599.0
2 2 1.85 2429.0
3 3 4.80 2316.0
4 4 3.99 1109.0
5 5 1.20 1261.0
6 6 4.24 1068.0
7 7 3.07 1098.0
8 8 2.08 1215.0
9 9 4.09 1043.0
10 10 2.95 1176.0
11 11 3.96 1038.0
12 12 3.95 1119.0
13 13 3.92 1074.0
14 14 3.91 1076.0
15 15 1.50 1224.0
16 16 3.65 962.0
17 17 3.85 1039.0
18 18 3.82 1062.0
19 19 3.34 917.0